我想在左上角绘制几个图形,分别标记为“ A”,“ B”,“ C”。我找不到(或找不到)添加此文本字段(如标签)的解决方案。理想情况下,这会将整个尺寸向左扩展。任何帮助表示赞赏!
我要添加“ A”,情节部分是我已经拥有的。
这是一个示例数据集和我当前的代码:
Day=rep(1:6, times=15)
Colour=c("Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Blue","Blue","Blue","Blue","Blue","Blue",
"Purple","Purple","Purple","Purple","Purple","Purple",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow",
"Red", "Red","Red","Red","Red","Red",
"Green","Green","Green","Green","Green","Green",
"Yellow", "Yellow","Yellow","Yellow","Yellow","Yellow")
Values=rep(c(9,8,7,6,5,8,7,6,5,4,7,6,5,4,3), times=6)
Fruit=rep(c("Apple", "Banana", "Blueberry","Mango", "Melon", "Pear"),
times = c(18,18,12,18,18,6))
Data <-data.frame(Day, Fruit, Colour, Values) %>%
mutate(unten=Values-0.2, oben=Values+0.2) %>% filter(Day==1)
design <- theme( strip.background = element_blank(),
panel.background = element_blank(), panel.border=element_blank(),
panel.grid.major = element_blank(), strip.placement = "outside",
axis.line.x = element_line(colour = "white"),
axis.ticks.x=element_blank(), legend.position = "none",
axis.text.x=element_text(vjust=5), axis.title=element_text(size=15),
axis.title.y = element_text(
margin = margin(t = 0, r = 15, b = 0, l = 0)), strip.text = element_text(size = 15), axis.text=element_text(size=11),
plot.title = element_text(size = 18, face = "bold"))
ForPlot <- ggplot(Data, aes(Colour, Values), fill=Colour)
ylines <- c(0, 5, 10)
ForPlot + theme_classic() +
geom_hline(yintercept = ylines, colour= "lightgrey" )+
scale_y_continuous(breaks=ylines,
limits=c(0,10),
labels=ylines
)+
geom_col(position=position_dodge2 (preserve = "single"),
aes(fill=`Colour`),width=.7)+
ylab("Values") + xlab("Colour") +
ggtitle("Title")+
facet_grid(~`Fruit`, scales = "free_x", space = "free_x", switch = "y") +
design +
scale_fill_manual("Colour", values=c('Blue', 'Green', 'Purple','Red', 'Yellow'))+
geom_text(aes(label = Values, y = Values), size=3, vjust = -.3) +
geom_segment(aes(x=0.4,xend=Inf, y=0, yend=0), color="black")
你好,谢谢你的回复。如何将grid.draw添加到绘图中,或者textGrob如何工作?我尝试了[plot ..... + grid.draw],但没有任何反应。
绘制绘图后,@ BeccaLi只需调用上面的函数即可。您无需将其添加到绘图中-调用此函数即可绘制绘图
知道了 在这种情况下,我以后如何保存地块?到目前为止,我将图分配给“ p”并使用了ggsave(plot,file =“ ..”)
@BeccaLi就是您保存基本R图的方式。调用
png("my_file.png")
THEN绘制您的图和注释,然后做dev.off()
,您的文件将在那里。您可以在中设置图的尺寸png
以及其他选项。或者,也可以保存为pdf()
或,而不是保存为png格式svg()
。如果您在R Studio中,还可以使用绘图窗口中的“导出”按钮,根据需要保存文件。