【问题标题】:ggplot2 in R: annotate outside of plot and underline textR中的ggplot2:在绘图之外注释并下划线文本
【发布时间】:2018-02-28 18:49:10
【问题描述】:

我已经为此苦苦思索了好几个小时。我到现在为止:

library(ggplot2)
library(grid)

all_data = data.frame(country=rep(c("A","B","C","D"),times=1,each=20),
                  value=rep(c(10,20,30,40),times=1,each=20),
                  year = rep(seq(1991,2010),4))


# PLOT GRAPH

p1 <- ggplot() + theme_bw() + geom_line(aes(y = value, x = year, 
colour=country), size=2,
                                    data = all_data, stat="identity") + 
theme(plot.title = element_text(size=18,hjust = -0.037), legend.position="bottom", 
    legend.direction="horizontal", legend.background = element_rect(size=0.5, linetype="solid", colour ="black"),
    legend.text = element_text(size=16,face = "plain"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    panel.border = element_blank(),axis.line = element_line(colour = "black"),legend.title = element_blank(),
    axis.text=element_text(size=18,face = "plain"),axis.title.x=element_text(size=18,face = "plain", hjust = 1,
                                                                             margin = margin(t = 10, r = 0, b = 0, l = 0)),
    axis.title.y=element_blank())


p1 <- p1 + ggtitle("Index")    
p1 <- p1 + xlab("Year")
p1 <- p1 + scale_x_continuous(expand=c(0,0),breaks=seq(1991,2010,4))

p1 <- p1 + theme(plot.margin=unit(c(5.5, 300, 5.5, 5.5), "points"))

p1 <- p1 + geom_text(aes(label = "Country", x = 2011, y = 
max(all_data$value)+10), hjust = 0, vjust = -2.5, size = 6)
p1 <- p1 + geom_text(aes(label =  "Average", x = Inf, y = 
max(all_data$value)+10), hjust = -1.5, vjust = -2, size = 6)

p1 <- p1 + geom_text(aes(label = all_data$country, x = 2011, y = 
all_data$value), hjust = 0, size = 6)
p1 <- p1 + geom_text(aes(label = as.character(all_data$value), x = Inf, 
y = all_data$value), hjust = -5, size = 6)


p1 <- p1 + 
annotate("segment",x=2011,xend=2014,y=Inf,yend=Inf,color="black",lwd=1)

# Override clipping 

gg2 <- ggplot_gtable(ggplot_build(p1))
gg2$layout$clip[gg2$layout$name == "panel"] <- "off"
grid.draw(gg2)

我正在努力解决以下问题:

1) 如何在绘图之外进行注释,在不延伸 x 轴的情况下在“国家/地区”和“平均值”下划线。

2) 整个注释过程没有更系统的方法吗?通过目测调整hjust和vjust似乎很麻烦。

感谢任何帮助!

【问题讨论】:

  • 如果您打开“输出”,您会注意到“国家/地区”和以下所有值(A、B、C、D)“扩展”了 x 轴。我想将它们绘制在 x 轴标题“Year”之后,并且 x 轴运行到 2009 年。其次,我想在上面强调“Country”和“Average”字符串,此刻我只在“国家/地区”下划线,而在“平均”下划线很困难。

标签: r ggplot2 annotate geom-text


【解决方案1】:

看看这是否适合你:

# define some offset parameters
x.offset.country = 2
x.offset.average = 5
x.range = range(all_data$year) + c(0, x.offset.average + 2)
y.range = range(all_data$value) + c(-5, 10)
y.label.height = max(all_data$value) + 8

# subset of data for annotation
all_data_annotation <- dplyr::filter(all_data, year == max(year))

p <- ggplot(all_data,
       aes(x = year, y = value, group = country, colour = country)) +
  geom_line(size = 2) +

  # fake axes (x-axis stops at year 2009, y-axis stops at value 45)
  annotate("segment", x = 1991, y = 5, xend = 2009, yend = 5) +
  annotate("segment", x = 1991, y = 5, xend = 1991, yend = 45) + 

  # country annotation
  geom_text(data = all_data_annotation, inherit.aes = FALSE,
            aes(x = year + x.offset.country, y = value, label = country)) +
  annotate("text", x = max(all_data$year) + x.offset.country, y = y.label.height, 
           label = "~underline('Country')", parse = TRUE) +

  # average annotation
  geom_text(data = all_data_annotation, inherit.aes = FALSE,
            aes(x = year + x.offset.average, y = value, label = value)) +
  annotate("text", x = max(all_data$year) + x.offset.average, y = y.label.height, 
           label = "~underline('Average')", parse = TRUE) +

  # index (fake y-axis label)
  annotate("text", x = 1991, y = y.label.height,
           label = "Index") +

  scale_x_continuous(name = "Year", breaks = seq(1991, 2009, by = 4), expand = c(0, 0)) +
  scale_y_continuous(name = "", breaks = seq(10, 40, by = 10), expand = c(0, 0)) +
  scale_colour_discrete(name = "") +
  coord_cartesian(xlim = x.range, ylim = y.range) +
  theme_classic() +
  theme(axis.line = element_blank(),
        legend.position = "bottom",
        legend.background = element_rect(size=0.5, linetype="solid", colour ="black"))

# Override clipping (this part is unchanged)
gg2 <- ggplot_gtable(ggplot_build(p))
gg2$layout$clip[gg2$layout$name == "panel"] <- "off"
grid.draw(gg2)

【讨论】:

  • 这似乎运作良好,非常感谢!最后一件事:如果我尝试使用 scale_colour_manual(values=colour) 自己定义颜色,其中颜色为: color
  • 愚蠢的我,这当然是传说中的标题。现在都修好了。
猜你喜欢
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 2021-01-05
  • 2016-10-27
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多