2016-02-03 48 views
1

我有这些数据被包括在所述柱的底部与dput()使用填充时在ggplot条形图标注每个条AES

> 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) 

enter image description here

不过,我想只有标有nCore的红柱d将蓝色条标记为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)) 
+2

不能你做另一个变量等于NCORE或NPeriph根据关于它们是否是红色/蓝色并将其用于标签? – jalapic

+0

不知道我完全按照...?发布为答案...? –

回答

2

添加ifelse()声明的要求

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) 
+0

当然完美。谢谢。 –

3

您可以到geom_text

+ geom_text(aes(label = ifelse(variable=="Core1Cone", round(nCore), round(nPeriph))), vjust=1.5, position=position_dodge(.9), size=5) 

enter image description here