使用多面条形图将文本添加到绘图中 [英] Add text to plot with facetted bar chart

查看:78
本文介绍了使用多面条形图将文本添加到绘图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与此问题有关.我希望在4年内实现"2014".我试图重复,但是我的代码没有给出我想要的东西.

在eipi10的帮助下,用于注释2014的新代码

  ann_text<-data.frame(x ="S4-conv",y = 1.75,lab ="2014",Rot.Herb= NA,值= NA,变量= NA,N = NA,sd = NA,se = NA,ci = NA,Rot = factor("4-year",levels = c("2-year","3-year","4-year"))) 

运行 p + geom_text(data = ann_text,label ="2014")后,我收到一条错误消息,提示 Error:将离散值提供给连续刻度.请查看我的代码和数据格式出了什么问题.谢谢.

解决方案

事实证明,问题是,当您在 ann_text 中包含 value = NA 时,它将被解释为逻辑(而不是数字,这是 Shannon.long2 中的模式),导致此错误,因为ggplot期望使用数字变量而不是分类变量.在中设置 value = NA_real _ (除了 NA ,R还具有特定于类的缺失值常量;有关更多信息,请参见?NA )> ann_text 以确保将 value 解释为数字并解决错误.或将 value 设置为任意数字,例如 value = 0 .

在下面的示例中,我删除了所有 theme lab 语句,以将代码缩短为基本内容:

  p = ggplot(Shannon.long2,aes(x = Rot.Herb,y = value,fill = factor(variable)))+geom_bar(stat ="identity",position ="dodge")+geom_errorbar(aes(ymin = value-se,ymax = value + se),size = 0.5,width = .25,position = position_dodge(.9))+facet_grid(〜Rot,scales ="free_x",space ="free_x")ann_text<-data.frame(x ="S4-conv",y = 1.75,lab ="2014",Rot.Herb = NA,值= NA_real_,变量= NA)p + geom_text(数据= ann_text,aes(label = lab,x,y)) 

请注意,您还需要将 x y 值提供给 geom_text 以提供标签位置.

另一种选择是仅使用与原始数据框中相同的x和y变量名称,因为ggplot已经知道这些名称并已基于它们缩放了图形.现在,我们需要添加的唯一缺少的列是 variable :

  ann_text<-data.frame(Rot.Herb ="S4-conv",值= 1.75,lab ="2014",变量= NA)p + geom_text(数据= ann_text,aes(标签=实验室,Rot.Herb,值)) 

My question is related to this question. I want "2014" in the 4-year facet. I tried to repeat but my code doesn't give what I want.

Annotating text on individual facet in ggplot2

This is my data

structure(list(Rot = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("2-year", 
"3-year", "4-year"), class = "factor"), Rot.Herb = structure(c(3L, 
3L, 4L, 4L, 13L, 13L, 14L, 14L, 5L, 5L, 6L, 6L, 9L, 9L, 10L, 
10L, 15L, 15L, 16L, 16L, 1L, 1L, 2L, 2L, 7L, 7L, 8L, 8L, 11L, 
11L, 12L, 12L, 17L, 17L, 18L, 18L), .Label = c("A4-conv", "A4-low", 
"C2-conv", "C2-low", "C3-conv", "C3-low", "C4-conv", "C4-low", 
"O3-conv", "O3-low", "O4-conv", "O4-low", "S2-conv", "S2-low", 
"S3-conv", "S3-low", "S4-conv", "S4-low"), class = "factor"), 
    variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Diversity", 
    "Evenness"), class = "factor"), N = c(4, 4, 4, 4, 4, 4, 4, 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
    4, 4, 4, 4, 4, 4, 4, 4, 4, 4), value = c(0.78537789925, 0.613408315, 
    1.305194686, 0.79519430975, 0.4481728555, 0.30608817425, 
    1.20978861475, 0.8580643725, 0.92387324875, 0.630166121, 
    0.945954185, 0.561172324, 1.43952456275, 0.8616864655, 1.23679146725, 
    0.831737624, 1.033474108, 0.80689293925, 0.9910142125, 0.79342098075, 
    1.175512223, 0.6293940245, 0.981614832, 0.62342189825, 1.351710013, 
    0.805075937, 1.6598348325, 0.7983622545, 1.01606920875, 0.5751418795, 
    1.0500365255, 0.56408326225, 1.07162937725, 0.6756859865, 
    0.45699816625, 0.44444147325), sd = c(0.354077266902404, 
    0.208934910331856, 0.169501822767995, 0.0774319459391732, 
    0.737366460962239, 0.40697977697835, 0.494107033311986, 0.11906912863268, 
    0.491492768082854, 0.34236657107712, 0.219739438843007, 0.205905593411204, 
    0.319301583035043, 0.0696484379979274, 0.0563293598951725, 
    0.0978700910274188, 0.446850757364563, 0.175073468716825, 
    0.426859848850874, 0.180469101499932, 0.526842123835502, 
    0.200470277385505, 0.574885944755375, 0.27189545397305, 0.39621771945215, 
    0.150798258847229, 0.275863362594154, 0.111178397407429, 
    0.254811233135664, 0.158920851982914, 0.198698241334475, 
    0.0730606635175717, 0.717706309307313, 0.453776579066358, 
    0.574276936403411, 0.513758415496589), se = c(0.177038633451202, 
    0.104467455165928, 0.0847509113839974, 0.0387159729695866, 
    0.368683230481119, 0.203489888489175, 0.247053516655993, 
    0.0595345643163399, 0.245746384041427, 0.17118328553856, 
    0.109869719421504, 0.102952796705602, 0.159650791517521, 
    0.0348242189989637, 0.0281646799475863, 0.0489350455137094, 
    0.223425378682282, 0.0875367343584126, 0.213429924425437, 
    0.090234550749966, 0.263421061917751, 0.100235138692753, 
    0.287442972377688, 0.135947726986525, 0.198108859726075, 
    0.0753991294236146, 0.137931681297077, 0.0555891987037145, 
    0.127405616567832, 0.0794604259914568, 0.0993491206672376, 
    0.0365303317587859, 0.358853154653656, 0.226888289533179, 
    0.287138468201705, 0.256879207748294), ci = c(0.563415944919255, 
    0.332462066715199, 0.26971522480343, 0.123211505132525, 1.1733145846647, 
    0.647595643784969, 0.786234551289211, 0.189465554245211, 
    0.782074671929471, 0.544781614588516, 0.349654482635521, 
    0.327641747494367, 0.508080071600555, 0.110826207087643, 
    0.089632581638694, 0.155733154793995, 0.71103927089404, 0.278580956835532, 
    0.679229274424713, 0.287166612643164, 0.838323385234058, 
    0.318992946792351, 0.914771825423139, 0.432646341459985, 
    0.630470808679215, 0.23995368085579, 0.438960169525453, 0.176909640028318, 
    0.40546153371869, 0.252878539112781, 0.316173242000635, 0.116255819336536, 
    1.14203089616693, 0.722059798737006, 0.91380275723334, 0.817504285602766
    )), .Names = c("Rot", "Rot.Herb", "variable", "N", "value", 
"sd", "se", "ci"), row.names = c(NA, -36L), class = "data.frame")

and the code to graph

p <- ggplot(Shannon.long2, aes(x=Rot.Herb, y=value, fill=factor(variable)))+
  geom_bar(stat="identity", position="dodge")+
  scale_fill_brewer(palette = "Set1")+
  theme_bw() +
  theme(panel.grid.major=element_blank()) +
  facet_grid(~Rot, scales = "free_x", space="free_x")+
  theme(legend.title=element_blank(),legend.text=element_text(size=20),legend.position="top")+
  geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.5, width=.25,position=position_dodge(.9))+
  xlab("\nTreatment") +
  theme(axis.title = element_text(size=24,face="bold", vjust=4), axis.text.x = element_text(size=20,angle = 90, hjust = 1)) +
  ylab("Shannon's H' and E'") +
  theme(axis.title = element_text(size=24,face="bold", vjust=2), axis.text.y = element_text(size=20, color="black"))+
  theme(strip.text.x = element_text(colour = "black", size = 20), strip.background = element_rect(fill = "white"))

produced graph (please don't mind the "2014" on the y-axis).

New code to annotate 2014, with help from eipi10

 ann_text <- data.frame(x = "S4-conv",y = 1.75,lab = "2014", Rot.Herb=NA, 
value=NA, variable=NA, 
N=NA, sd=NA, se=NA, ci=NA, 
Rot = factor("4-year",levels = c("2-year","3-year","4-year")))

I got an error saying Error: Discrete value supplied to continuous scale after I run p + geom_text(data = ann_text,label = "2014"). Please see what have been wrong with my code and data format. Thanks.

解决方案

It turns out the issue is that when you include value=NA in ann_text it gets interpreted as logical (rather than numeric, which is its mode in Shannon.long2), causing the error because ggplot expects a numeric variable rather than a categorical one. Set value=NA_real_ (in addition to NA, R has class-specific missing value constants; see ?NA for more info) in ann_text to ensure value is interpreted as numeric and resolve the error. Or set value to any number, e.g., value=0.

In the example below, I've removed all of the theme and lab statements to shorten the code down to the essentials:

p = ggplot(Shannon.long2, aes(x=Rot.Herb, y=value, fill=factor(variable))) +
  geom_bar(stat="identity", position="dodge") +
  geom_errorbar(aes(ymin=value-se, ymax=value+se), size=0.5, width=.25,position=position_dodge(.9)) +
  facet_grid(~Rot, scales = "free_x", space="free_x")

ann_text <- data.frame(x = "S4-conv", y = 1.75, lab = "2014", Rot.Herb=NA, 
                       value=NA_real_, variable=NA)

p + geom_text(data = ann_text, aes(label=lab, x, y))

Note that you also need to feed x and y values to geom_text to provide the label location.

Another option would be to just use the same x and y variable names as in your original data frame, since ggplot already knows these names and has scaled the graph based on them. Now the only missing column we need to add is variable:

ann_text <- data.frame(Rot.Herb = "S4-conv", value = 1.75, lab = "2014", variable=NA)

p + geom_text(data = ann_text, aes(label=lab, Rot.Herb, value))

这篇关于使用多面条形图将文本添加到绘图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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