R 如何在ggplot中使用axis.text.y旋转y轴上的特定元素/标签?

R 如何在ggplot中使用axis.text.y旋转y轴上的特定元素/标签?,r,ggplot2,R,Ggplot2,请在下面查找我的数据 我制作了这个情节: 我希望y轴上环绕的“20%点增加”-文本以角度=90旋转,但将剩余文本保持在角度=0 我用过这个脚本 ggplot(as.data.frame(nd), aes(x = lnd)) + geom_area(aes(y = y), data = function(x) subset(x, lnd >= 0 & lnd <=10),fill="#2C77BF", alpha=0.3) + geom_area(aes(y =

请在下面查找我的数据

我制作了这个情节:

我希望y轴上环绕的“20%点增加”-文本以
角度=90旋转,但将剩余文本保持在
角度=0

我用过这个脚本

ggplot(as.data.frame(nd), aes(x = lnd))  +


  geom_area(aes(y = y), data = function(x) subset(x, lnd >= 0 & lnd <=10),fill="#2C77BF", alpha=0.3) +
  geom_area(aes(y = y), data = function(x) subset(x, lnd >= 10 & lnd <= 25),fill="#E38072", alpha=0.3) +
  geom_area(aes(y = y), data = function(x) subset(x, lnd >= 25 & lnd <= 100),fill="#6DBCC3", alpha=0.3) +

  geom_line(aes(y = y, col = lnd),size=1) +

  geom_segment(aes(x = 0, y = 0.3479943, xend = 0, yend = 0.2), lty="solid", size=0.9, color="black") +
  geom_point(mapping = aes(x = 0, y = 0.3479943), size=4, shape=20, col="black", alpha=0.5) +

  ggtitle("", subtitle = "Risk") + scale_x_continuous(name="", breaks=seq(0,100,by=10), limits=c(0,100), label=c("LND 0% \nn=664","10%","20%", "30%", "40%","50%","60%","70%","80%","90%","100%")) +


  scale_y_continuous(name = "", breaks = seq(0.35,1,by=.1), labels=c("35%","20%-point\nincrease","55%","20%-point\nincrease","75%","20%-point\nincrease","95%")) +

  coord_cartesian(ylim=c(0.25,1)) +

  annotate("text", x = 5, y = 0.3, label = "LND 0 - \u226410%\nn=427", fontface=2,cex=3.5, colour="#2C77BF") +
  annotate("text", x = 17.5, y = 0.3, label = "LND 10 - 25%\nn=195", fontface=2,cex=3.5, colour="#E38072") +
  annotate("text", x = 30, y = 0.3, label = "LND \u226525%\nn=91", fontface=2,cex=3.5, colour="#6DBCC3") +


theme(axis.text.x = element_text(colour="grey20",size=11,face=c("bold","plain","plain","plain","plain","plain","plain","plain","plain","plain","plain")), 
      axis.title.x = element_text(color = "grey20", size = 11, face="bold", margin=ggplot2::margin(t=12)),
      axis.text.y = element_text(color = c("grey","black","grey","black","grey","black","grey"),angle = 0, size = 11), 
      axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
      plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
      plot.subtitle = element_text(hjust = 0.5),
      legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="none") 

首先,我冒昧地重新组织了一点数据帧,以简化绘图代码:

library(tidyverse)

nd2 <- nd %>% mutate(lnd_cat = as.factor(case_when(lnd >= 0 & lnd <=10 ~ 1,
                                  lnd >= 10 & lnd <=25 ~ 2,
                                  lnd >= 25 & lnd <= 100 ~ 3)))

我知道解决方案有点混乱,但是,它是有效的

library(tidyverse)

nd2 <- nd %>% mutate(lnd_cat = as.factor(case_when(lnd >= 0 & lnd <=10 ~ 1,
                                  lnd >= 10 & lnd <=25 ~ 2,
                                  lnd >= 25 & lnd <= 100 ~ 3)))
ggplot(data = nd2, aes(x = lnd, y=y))  +
  geom_area(aes(group = lnd_cat, fill = lnd_cat, alpha = 0.3)) +
  scale_fill_manual(values = c("#2C77BF","#E38072", "#6DBCC3")) +
  geom_line() +
  ggtitle(label = " ", subtitle = "Risk") + 
  scale_x_continuous(breaks=seq(0,100,by=10), lim = c(0,100), label=c("LND 0% \nn=664","10%","20%", "30%", "40%","50%","60%","70%","80%","90%","100%")) +
  scale_y_continuous(breaks = seq(0.35,1,by=.1), labels=c("35%","20%-point\nincrease","55%","20%-point\nincrease","75%","20%-point\nincrease","95%")) +

  coord_cartesian(ylim=c(0.25,1)) +

  annotate("text", x = c(5, 17.5, 30), y = 0.3, 
           label = c("LND 0 - \u226410%\nn=427","LND 10 - 25%\nn=195","LND \u226525%\nn=91"), 
           fontface=2, cex=3.5, 
           colour=c("#2C77BF","#E38072", "#6DBCC3" )) +

  theme(axis.text.x = element_text(colour="grey20",face=c("bold", rep("plain", 10))), 
        axis.text.y = element_text(color = c(rep(c("grey","black"), 3) ,"grey"), 
        angle = c(0, 90), hjust = 0.5),   # <-- the integer vector will be reused
        axis.title = element_blank(),
        plot.subtitle = element_text(hjust = 0.5),
        legend.position="none")