我怎样才能在ggplot2 / R中的绘图轴限制之外添加垂直线和水平文本? [英] How can I add vertical lines and horizontal text out of the plot axis limits in ggplot2/R?

查看:2024
本文介绍了我怎样才能在ggplot2 / R中的绘图轴限制之外添加垂直线和水平文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要由ggplot2制作的barplot。我想要添加两条垂直线,并在x轴下面添加一些文字。

$ $ $ $ $ $ $ $ $ $ $ $ b $ < 3L,5L,6L,8L,11L,13L,12L,10L,9L,7L),.Label = c(Bahr等人,Fuller等人,Garbossa等人,Gokhale等人, Iuchi等,Lee等,Lee Y等,Merrel等,Newton等,Rossetti等,Usery等,Wychowski等, (例如Zachenhofer等人),class =factor),nAE = c(-22L,-34L,-158L,-90L,-70L,-41L,-48L,-32L,-73L,-23L,-25L, (3L,1L,7L,1L,3L,10L,3L,6L,3L,5L,4L,6L,5L),SAE = c(0L,1L,0L,0L, 0L,0L,0L,2L,0L,2L,0L,0L)),.Names = c(author,nAE,AE,SAE),class =data.frame, row.names = c(NA,-13L))

代码到我的barplot:

 图书馆(dplyr)
图书馆(tidyr)
图书馆(ggplot2)

类别< ; - c(#f6766d,#01bfc4,orange)


q < - d% gt;%
gather(key,value,-author)%>%
ggplot(aes(author,value,fill = key))+
geom_col(alpha = 0.9)+
scale_x_discrete(name =Author)+
scale_y_continuous(name =Number of observations,limits = c(-160,15),
seq(-160,15,by = 10 )+
theme_grey()+
主题(legend.position =top)+
scale_fill_manual(labels = categories,values = cols)+
labs(fill = )

我附上了一张如何让我的barplot看起来像的照片。正如你所看到的,我添加了两条垂直线(随机位置)和三条文本(位于photoshop)。

stack.imgur.com/vTX3p.pngrel =nofollow noreferrer>



在此先感谢您,
C。

解决方案

垂直线完全没有问题。只需使用:

  + 
geom_vline(xintercept = 3.5,color =red)+
geom_vline (xintercept = 10.5,color =red)

值3.5和10.5表示行截距第三和第四个之间的x轴和第十和第十一个作者之间的x轴。

尽管在剧情之外添加文字是一个完全不同的野兽。我能想到的最干净的方式是在剧情内添加文本:

  y < -  min(d $ nAE )+ 10 
textaes< - data.frame(y = c(y,y,y),
x = c(2,7,12),
lab = c(Text1 (key,value,作者)%>%
ggplot(aes(x =作者,y = value,fill = key))+
geom_col(alpha = 0.9)+
scale_x_discrete(name =Author)+
scale_y_continuous(name =Number of observations,限制= c(-160,15),
seq(-160,15,by = 10),expand = c(0.15,0.05))+
theme_grey()+
主题legend.position =top,
axis.text.x = element_text(angle = 90,hjust = 1))+
scale_fill_manual(labels = categories,values = cols)+
labs (fill =)+
geom_vline(xintercept = 3.5,color =red)+
geom_vline(xintercept = 10.5,color =red)+
geom_text(mapping = aes (y = y,x = x,label = lab),
data = textaes,inherit.aes = FALSE)
q



编辑:刚刚找到一个相对简单的方法来添加文字以外的剧情


I have a barplot made primarily in ggplot2. I want two vertical lines added and some text below the x-axis.

#Load data
d <- structure(list(author = structure(c(1L, 2L, 4L, 3L, 5L, 6L, 8L, 11L, 13L, 12L, 10L, 9L, 7L), .Label = c("Bahr et al", "Fuller et al", "Garbossa et al", "Gokhale et al", "Iuchi et al", "Lee et al", "Lee Y et all", "Merrel et al", "Newton et al", "Rossetti et al", "Usery et al", "Wychowski et al", "Zachenhofer et al"), class = "factor"), nAE = c(-22L, -34L, -158L, -90L, -70L, -41L, -48L, -32L, -73L, -23L, -25L, -13L, -46L), AE = c(3L, 1L, 7L, 1L, 3L, 10L, 3L, 6L, 3L, 5L, 4L, 6L, 5L), SAE = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 2L, 0L, 0L)), .Names = c("author", "nAE", "AE", "SAE"), class = "data.frame", row.names = c(NA, -13L))

Code to my barplot:

library(dplyr)
library(tidyr)
library(ggplot2)

categories <- c("Adverse Effect", "No adverse effects", "Severe side effects")
cols <- c("#f6766d", "#01bfc4", "orange")


q <- d %>% 
gather(key, value, -author) %>% 
ggplot(aes(author, value, fill = key)) +
geom_col(alpha=0.9) + 
scale_x_discrete(name="Author") +
scale_y_continuous(name="Number of observations", limits=c(-160,15), 
seq(-160, 15, by=10)) +
theme_grey() +
theme(legend.position = "top") +
scale_fill_manual(labels = categories, values = cols) + 
labs(fill = "")

I have attached a picture below of how I want my barplot to look like. As you can see, I have added two vertical lines (at random position) and three texts (in photoshop).

Thanks in advance, C.

解决方案

The vertical lines are no problem at all. Simply use:

  +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red")

The values 3.5 and 10.5 mean that the lines intercept the x-axis between the third and fourth and respective between the tenth and eleventh author.

Adding text outside the plot is a whole different beast though. The "cleanest" way I could think of is adding the text inside the plot:

y <- min(d$nAE) + 10
textaes <- data.frame(y = c(y, y, y),
                      x = c(2, 7, 12),
                      lab = c("Text1", "Text2", "Text3"))
q <- d %>% 
  gather(key, value, -author) %>% 
  ggplot(aes(x=author, y=value, fill = key)) +
  geom_col(alpha=0.9) + 
  scale_x_discrete(name="Author") +
  scale_y_continuous(name="Number of observations", limits=c(-160,15), 
                     seq(-160, 15, by=10), expand = c(0.15, 0.05)) +
  theme_grey() +
  theme(legend.position = "top",
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(labels = categories, values = cols) + 
  labs(fill = "") +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red") +
  geom_text(mapping = aes(y = y, x = x, label = lab), 
            data = textaes, inherit.aes = FALSE)
q

EDIT: Just found a relatively easy way to add text outside the plot here. But I don't think it's a very nice solution:

q <- d %>% 
  gather(key, value, -author) %>% 
  ggplot(aes(x=author, y=value, fill = key)) +
  geom_col(alpha=0.9) + 
  scale_x_discrete(name="Author") +
  scale_y_continuous(name="Number of observations", limits=c(-160,15), 
                     seq(-160, 15, by=10), expand = c(0.15, 0.05)) +
  theme_grey() +
  theme(legend.position = "top",
        axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_fill_manual(labels = categories, values = cols) + 
  labs(fill = "") +
  geom_vline(xintercept= 3.5, colour = "red") + 
  geom_vline(xintercept= 10.5, colour = "red")
q

library(grid)
q
grid.text("Text1", x = unit(0.15, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))
grid.text("Text2", x = unit(0.5, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))
grid.text("Text3", x = unit(0.85, "npc"), y = unit(0.1, "npc"), gp=gpar(col="red"))

这篇关于我怎样才能在ggplot2 / R中的绘图轴限制之外添加垂直线和水平文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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