根据分组ggplot2对条纹进行着色和着色/纹理 [英] Colouring and shading/texture of bars according to grouping ggplot2

查看:1065
本文介绍了根据分组ggplot2对条纹进行着色和着色/纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图制作一个条形图,就像在下面的示例数据和脚本中那样,在给予治疗之前和之后,从4个人那里获取一个测量值。对不起,我没有足够的信誉上传示例图片。



我想用某种颜色来绘制个体(例如Tom as green,Fred red等),但对于代表我想添加的处理的条形图对角线上的颜色表示治疗。



这很容易吗?下面的代码我改编自:用不同颜色生成ggplot2 boxplot对于多个团体



非常感谢任何建议。



示例数据(对不起,不知道如何上传,以便从.csv导入):



pre $ 0,P,3,20.1341,1.049791,20,0.5728394,0.5569923
Fred,1,0,P,3,38.63702,1.042969,37.74,0.9499892,0.9271918
Tom,6,0,P, 3,42.3231,1.073583,43.75,1.7710033,1.6998725
Peter,6,0,P,3,36.01035,1.208213,35.63,4.1551262,3.7252776
Ted,1,1,P,3,22.79528, 1.110182,21.64,1.4179833,1.334943
Fred,1,1,P,3,24.25966,1.156925,23.82,2.1300073,1.9580866
Tom,6,1,P,3,13.78995,1.170568,13.15, 1.3126463,1.1985573
Peter,6,1,P,3,23.3236,1.4403,20.65,5.4688355,4.4300848

和我一直使用的代码:

  g < -  ggplot(例如,aes(x = Name,y =均值,fill = interaction(Name,Dose)))
g <-g + geom_bar(stat =identity,position =dodge)
g <-g + geom_errorbar(aes(ymax = Mean + Upper.SEM,ymin = Mean-Lower.SEM) ,位置= position_dodge(0.9),width = 0.5)
g <-g + scale_fill_manual(values = c(green,green,red,red,green,green red,red
))
g


解决方案

你似乎想要使用纹理,并且正如@ Henrik的评论中的链接所解释的,这在 ggplot 中是不可能的。一种可能的解决方法是为名称使用不同的颜色,并为剂量使用不同的颜色。

 库(ggplot2)
库(RColorBrewer)
categories< - aggregate(剂量〜名称,示例,函数(x)长度(unique(x)))#每个类别的子类别数量
category.palettes< - c( Purple,Reds,Greens,Blues)
colors< - unlist(lapply(1:nrow(categories),
function(i){colorRampPalette(brewer.pal ,category.palettes [i])[3:7])(categories [i,2])}))
names< - sort(unique(example $ Name))

g < - ggplot(例如,aes(x = Name,y = Mean,fill = interaction(Dose,Name)))
g < - g + geom_bar(stat =identity,position =dodge)
g <-g + geom_errorbar(aes(ymax =平均值+ Upper.SEM,ymin =平均下限-SEM),position = position_dodge(0.9),width = 0.5)
g <-g + scale_fill_manual (主题,值=颜色,布雷亚ks = paste0(1。,names),labels = names)
g


I am trying to make a bar plot like where, as with the example data and script below, a measurement is taken from 4 individuals say before and after giving a treatment. Sorry, I don't have enough reputation to upload an example picture.

I would like to plot the individuals by a certain colour (e.g. Tom as green, Fred red, etc) but then for the bars representing the treatment I would like to add diagonal lines on top of the colour to indicate treatment.

Is this possible easily? The code below I have adapted from: Generate ggplot2 boxplot with different colours for multiple groups

Many thanks in advance for any suggestions. I hope what I'd like to do makes sense.

Example data (sorry, not sure how to upload it so imported from .csv):

Name,Time,Dose,Variable,n,Mean,SD,Median,Upper.SEM,Lower.SEM
Ted,1,0,P,3,20.1341,1.049791,20,0.5728394,0.5569923
Fred,1,0,P,3,38.63702,1.042969,37.74,0.9499892,0.9271918
Tom,6,0,P,3,42.3231,1.073583,43.75,1.7710033,1.6998725
Peter,6,0,P,3,36.01035,1.208213,35.63,4.1551262,3.7252776
Ted,1,1,P,3,22.79528,1.110182,21.64,1.4179833,1.334943
Fred,1,1,P,3,24.25966,1.156925,23.82,2.1300073,1.9580866
Tom,6,1,P,3,13.78995,1.170568,13.15,1.3126463,1.1985573
Peter,6,1,P,3,23.3236,1.4403,20.65,5.4688355,4.4300848

And code I have been using:

g<- ggplot(example, aes(x=Name, y=Mean,fill=interaction(Name,Dose) ))
g<-g + geom_bar(stat="identity", position="dodge") 
g<-g + geom_errorbar(aes(ymax=Mean+Upper.SEM, ymin=Mean-Lower.SEM),       position=position_dodge(0.9), width=0.5)
g<- g+ scale_fill_manual(values=c("green","green","red","red","green","green","red","red"
                    ))
g

解决方案

You seem to want to use textures, and as the link in @Henrik's comment explains, that's not possible in ggplot. One possible work-around is to use different colors for the names, and different shades of color for the doses.

library(ggplot2)
library(RColorBrewer)
categories         <- aggregate(Dose~Name,example,function(x)length(unique(x)))  # number of sub-category for each category
category.palettes  <- c("Purples","Reds","Greens","Blues")
colors <- unlist(lapply(1:nrow(categories),
                        function(i){colorRampPalette(brewer.pal(9,category.palettes[i])[3:7])(categories[i,2])}))
names <- sort(unique(example$Name))

g <- ggplot(example, aes(x=Name, y=Mean,fill=interaction(Dose,Name) ))
g <- g + geom_bar(stat="identity", position="dodge") 
g <- g + geom_errorbar(aes(ymax=Mean+Upper.SEM, ymin=Mean-Lower.SEM), position=position_dodge(0.9), width=0.5)
g <- g+ scale_fill_manual("Subject", values=colors, breaks=paste0("1.",names),labels=names)
g

这篇关于根据分组ggplot2对条纹进行着色和着色/纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆