我有这些数据,这些数据包含在dput()
> head(df)
variable value nCore nPeriph FK90
1 Core1Cone 88.00222 359.65000 97.71667 24.28
2 Core1Cone 78.26995 79.76667 42.55000 39.90
3 Core1Cone 76.16143 461.71667 102.28333 42.56
4 Core1Cone 68.66943 209.43333 45.98333 78.31
5 Core1Cone 34.65022 487.68333 38.10000 99.27
6 Core1Cone 53.46456 250.63333 61.38333 103.93
使用下面的代码,我可以制作下图,其中条形标有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))
答案 0 :(得分:3)
您可以向ifelse()
geom_text
语句
+ geom_text(aes(label = ifelse(variable=="Core1Cone", round(nCore), round(nPeriph))), vjust=1.5, position=position_dodge(.9), size=5)
答案 1 :(得分:2)
按要求
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)