R 使用fill aes时标记ggplot条形图中的每个条形图
我有这些数据,它们用R 使用fill aes时标记ggplot条形图中的每个条形图,r,ggplot2,R,Ggplot2,我有这些数据,它们用dput() 使用下面的代码,我可以制作下图,其中条形图标有nCore library(ggplot2) ggplot(df, aes(x=as.factor(FK90), y=value, fill=variable)) + geom_bar(stat="identity", position="dodge", colour="black")+ guides(fill=guide_legend(title=NULL))+ scale_fill_manual
dput()
使用下面的代码,我可以制作下图,其中条形图标有nCore
library(ggplot2)
ggplot(df, aes(x=as.factor(FK90), y=value, fill=variable)) +
geom_bar(stat="identity", position="dodge", colour="black")+
guides(fill=guide_legend(title=NULL))+
scale_fill_manual(values=c("#CC3322", "#66CCFF"), labels=c("Core", "Periphery"))+
theme(plot.title = element_text(size = 24, face = "bold"), legend.text=element_text(size=16), legend.position=c(0.88,0.91),
axis.title.x=element_text(size=18), axis.title.y=element_text(size=18), axis.text=element_text(size=14))+
scale_x_discrete(labels=df$FK90)+
geom_text(aes(label = round(nCore)),vjust=1.5,position=position_dodge(.9),size=5)
但是,我只希望用nCore
标记的红色条和用nPeriph
字段标记的蓝色条
如果您对如何给每个酒吧贴上不同的标签有任何建议,我们将不胜感激
df <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Core1Cone",
"Outer1Cone"), class = "factor"), value = c(88.0022244, 78.269954,
76.1614266, 68.6694254, 34.650217, 53.4645565, 36.4734627, 36.1200483,
24.9019273, 81.786134, 46.4070393, 14.1718014, 27.5590551, 32.0934021,
0, 68.9482225), nCore = c(359.649999823245, 79.7666666746877,
461.716666608, 209.433333469832, 487.683333152032, 250.633333316376,
122.783333278669, 331.533333438411, 359.649999823245, 79.7666666746877,
461.716666608, 209.433333469832, 487.683333152032, 250.633333316376,
122.783333278669, 331.533333438411), nPeriph = c(97.716666876755,
42.5500000253123, 102.283333392, 45.9833332301678, 38.1000001479679,
61.3833333836244, 12.7500000213309, 112.983333261589, 97.716666876755,
42.5500000253123, 102.283333392, 45.9833332301678, 38.1000001479679,
61.3833333836244, 12.7500000213309, 112.983333261589), FK90 = c(24.28,
39.9, 42.56, 78.31, 99.27, 103.93, 135.34, 169.19, 24.28, 39.9,
42.56, 78.31, 99.27, 103.93, 135.34, 169.19)), .Names = c("variable",
"value", "nCore", "nPeriph", "FK90"), class = "data.frame", row.names = c(NA,
-16L))
df您可以在geom\u文本中添加ifelse()
语句
+ geom_text(aes(label = ifelse(variable=="Core1Cone", round(nCore), round(nPeriph))), vjust=1.5, position=position_dodge(.9), size=5)
根据要求
df$newvar <- ifelse(df$variable=='Core1Cone', df$nCore, df$nPeriph)
library(ggplot2)
ggplot(df, aes(x=as.factor(FK90), y=value, fill=variable)) +
geom_bar(stat="identity", position="dodge", colour="black")+
guides(fill=guide_legend(title=NULL))+
scale_fill_manual(values=c("#CC3322", "#66CCFF"), labels=c("Core", "Periphery"))+
theme(plot.title = element_text(size = 24, face = "bold"), legend.text=element_text(size=16), legend.position=c(0.88,0.91),
axis.title.x=element_text(size=18), axis.title.y=element_text(size=18), axis.text=element_text(size=14))+
scale_x_discrete(labels=df$FK90)+
geom_text(aes(label = round(newvar)),vjust=1.5,position=position_dodge(.9),size=5)
df$newvar您不能根据颜色是否为红色/蓝色,创建另一个等于nCore或NPeriph的变量,并将其用于标记吗?我不确定是否完全遵循。。。?作为答案发布。。。?
df$newvar <- ifelse(df$variable=='Core1Cone', df$nCore, df$nPeriph)
library(ggplot2)
ggplot(df, aes(x=as.factor(FK90), y=value, fill=variable)) +
geom_bar(stat="identity", position="dodge", colour="black")+
guides(fill=guide_legend(title=NULL))+
scale_fill_manual(values=c("#CC3322", "#66CCFF"), labels=c("Core", "Periphery"))+
theme(plot.title = element_text(size = 24, face = "bold"), legend.text=element_text(size=16), legend.position=c(0.88,0.91),
axis.title.x=element_text(size=18), axis.title.y=element_text(size=18), axis.text=element_text(size=14))+
scale_x_discrete(labels=df$FK90)+
geom_text(aes(label = round(newvar)),vjust=1.5,position=position_dodge(.9),size=5)