-1

我正在尝试复制条形图,如下所示。

这是数据框的示例。其中 y 变量是tasa, x 变量是year,每个 x 刻度标签的文本中显示的数字是inscripciones

df <- structure(list(year = c("2018", "2019"), inscripciones = c(3038910, 3680696), tasa = c(88.9528707645112, 104.692208214133)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -2L))

p <- ggplot(data = df, aes(x = year, y = tasa)) +
  geom_bar(width = 0.4, stat = "identity", fill="orange")+
  geom_text(aes(year, tasa + 5, label = round(tasa,2), fill = NULL), size=4)+
  labs(x = NULL, y = NULL)+
  scale_y_continuous(breaks = seq(0, 110, by = 10))+
  theme_bw() 

如何将这些长文本(包括数据框中的信息)添加到 x 刻度标签?

图形

4

1 回答 1

1

首先,您的数据和绘图组合不可重现。我重命名annohyearthen create the plot p

然后,scale_x_discrete当你想跳过行时,使用 "\n" 字符串;

在此处输入图像描述

long_text_1 <-  'Gün, senden ışık alsa\n da bir renge bürünse;\n
Ay, secde edip çehrene,\n yerlerde sürünse;\n
Her şey silinip\n kayboluyorken nazarımdan,\n
Yalnız o yeşil\n gözlerinin nuru görünse...'

long_text_2 <- 'Ruhun mu ateş,\nyoksa o gözler mi alevden?\n
Bilmem bu yanardağ\n ne biçim korla tutuştu?\n
Pervane olan kendini\n gizler mi hiç alevden?\n
Sen istedin ondan bu\n gönül zorla tutuştu.'


p <- ggplot(data = df, aes(x = year, y = tasa)) +
  geom_bar(width = 0.4, stat = "identity", fill="orange")+
  geom_text(aes(year, tasa + 5, label = round(tasa,2), fill = NULL), size=4)+
  labs(x = NULL, y = NULL)+
  scale_y_continuous(breaks = seq(0, 110, by = 10))+
  theme_bw()+
  scale_x_discrete(labels=c('2018'=long_text_1,'2019'=long_text_2))
于 2021-09-13T17:28:45.433 回答