本文介绍了梯度填充条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在查看不同人群的行为(在此数据集中称为群集)以及他们对使用的浏览器类型的偏好。我想创建一个条形图,显示使用每种类型浏览器的每个群集的百分比。 以下是生成类似数据集的一些代码(请忽略每个群集的百分比不会加起来为1): $ p $ browserNames< -c(microsoft,mozilla ,google) clusterNames< - c(Cluster 1,Cluster 2,Cluster 3) percentages myData< -as.data.frame(list(browserNames = rep(browserNames,3), clusterNames = rep(clusterNames,each = 3),百分比=百分比)) 这里是我已经能够拿出我想要的图表: ggplot(myData,aes(x = browserNames,y = percentages, fill = factor(clusterNames)))+ geom_bar(stat =identity,position =dodge)+ scale_y_continuous(name =Perc ent Weight,labels = percent) 我希望每个群集的填充都是渐变填充我确定的高和低值。因此,在这个例子中,我希望能够为每个代表的群集设置3个高值和低值。 我对不同的scale_fill命令,我已经足够新ggplot,我很确定我可能只是做错了。任何想法? 编辑:这里是我正在寻找的图片: (原始图片可在 https://www.dropbox.com/s/py6hifejqz7k54v/gradientExample.bmp )解决方案这与您的想法相近吗? gg< - with(myData,myData [order(browserNames) (brewer.pal(3,Reds),brewer.pal(3,Greens)),其中, ,brewer.pal(3,Blues)) ggplot(zz,aes(x = browserNames,y =百分比, fill =因子(颜色),group =因子(clusterNames ))+ geom_bar(stat =identity,position =dodge,color =grey70)+ scale_ fill_manual(Cluster,values = colors, breaks = c(3,6,9),labels = c(Google,Microsoft,Mosilla)) #颜色集取决于集群库(RColorBrewer)#for brewer.pal(...) gg < - with(myData,myData [order(clusterNames,percentages),]) gg $ colors< - 1:9 col ggplot(gg,aes(x = browserNames,y =百分比, fill = factor(colors),group = factor(clusterNames)))+ geom_bar(stat =identity,position =dodge,color =grey70)+ scale_fill_manual(Cluster ,values = col, breaks = c(3,6,9),labels = c(Cluster1,Cluster2,Cluster3)) pre> I'm looking at behavior of different groups of people (called Clusters in this data set) and their preference for the type of browser they use. I want to create a bar graph that shows the percentage of each cluster that is using each type of browser.Here is some code to generate a similar dataset (please ignore that the percentages for each cluster will not add up to 1):browserNames <- c("microsoft","mozilla","google")clusterNames <- c("Cluster 1","Cluster 2","Cluster 3")percentages <- runif(n=length(browserNames)*length(clusterNames),min=0,max=1)myData<-as.data.frame(list(browserNames=rep(browserNames,3), clusterNames=rep(clusterNames,each=3), percentages=percentages))Here's the code I've been able to come up with so far to get the graph I desire:ggplot(myData, aes(x=browserNames, y=percentages, fill=factor(clusterNames))) + geom_bar(stat="identity",position="dodge") + scale_y_continuous(name="Percent Weight", labels=percent)I want the fill for each cluster to be a gradient fill with high and low values that I determine. So, in this example, I would like to be able to set 3 high and low values for each cluster that is represented.I've had trouble with the different scale_fill commands, and I'm new enough to ggplot that I am pretty sure I'm probably just doing it wrong. Any ideas?Edit: Here is a picture of what I'm looking for:(Original image available at https://www.dropbox.com/s/py6hifejqz7k54v/gradientExample.bmp) 解决方案 Is this close to what you had in mind??# color set depends on browserlibrary(RColorBrewer) # for brewer.pal(...)gg <- with(myData, myData[order(browserNames,percentages),])gg$colors <- 1:9colors <- c(brewer.pal(3,"Reds"),brewer.pal(3,"Greens"),brewer.pal(3,"Blues"))ggplot(zz, aes(x=browserNames, y=percentages, fill=factor(colors), group=factor(clusterNames))) + geom_bar(stat="identity",position="dodge", color="grey70") + scale_fill_manual("Cluster", values=colors, breaks=c(3,6,9), labels=c("Google","Microsoft","Mosilla"))# color set depends on clusterlibrary(RColorBrewer) # for brewer.pal(...)gg <- with(myData, myData[order(clusterNames,percentages),])gg$colors <- 1:9col <- c(brewer.pal(3,"Reds"),brewer.pal(3,"Greens"),brewer.pal(3,"Blues"))ggplot(gg, aes(x=browserNames, y=percentages, fill=factor(colors), group=factor(clusterNames))) + geom_bar(stat="identity",position="dodge", color="grey70") + scale_fill_manual("Cluster", values=col, breaks=c(3,6,9), labels=c("Cluster1","Cluster2","Cluster3")) 这篇关于梯度填充条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 14:01