如何在ggplot中使用axis.text.y替换文本标签的文本角度/旋转?

时间:2019-12-19 22:07:06

标签: r ggplot2

请在下面找到My Data

我制作了这个情节:

enter image description here

我希望在y轴上将圈出的“增加20%点”文本旋转angle=90,但将其余文本保留在angle=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") 

我只是尝试了...,angle=c(0,90,0,90,0,90,0),...,但是收到了以下错误消息:“如果(0 <= angle&angle <90){:.... length> 1并且仅使用第一个元素“

关于如何倾斜某些y轴文本的任何解决方案?

My data

nd <- structure(list(y = c(0.347994323433218, 0.374015418531899, 0.399629966119251, 
                     0.421742599051725, 0.44036657229005, 0.457260948328608, 0.474417648309559, 
                     0.493640865541709, 0.51488973428659, 0.537423493454801, 0.560430703662936, 
                     0.583045245963954, 0.604403317363394, 0.624118977779026, 0.642197094829998, 
                     0.658675856783435, 0.673612299496595, 0.687077654264746, 0.699153267191595, 
                     0.709927163750076, 0.719491270455802, 0.727939260908065, 0.735364964416906, 
                     0.741861259442995, 0.747519368138831, 0.752428469501541, 0.756675554521809, 
                     0.760345455237322, 0.763520989282281, 0.766283171326885, 0.768711452044572, 
                     0.770883953521034, 0.772877677101532, 0.774768665456197, 0.776627848464512, 
                     0.778481560816502, 0.780330067279974, 0.782173296838651, 0.784011178693879, 
                     0.785843642275446, 0.787670617252314, 0.78949203354347, 0.791307821328959, 
                     0.793117911060613, 0.794922233473425, 0.796720719596049, 0.798513300762498, 
                     0.800299908622762, 0.802080475154149, 0.803854932672504, 0.805623213843368, 
                     0.807385251693197, 0.809140979620611, 0.810890331407796, 0.812633241231531, 
                     0.814369643674837, 0.816099473738181, 0.81782266685046, 0.819539158880875, 
                     0.821248886149957, 0.822951785441022, 0.824647794011433, 0.826336849604117, 
                     0.828018890458497, 0.829693855322157, 0.831361683462205, 0.833022314676256, 
                     0.834675689303694, 0.836321748237183, 0.837960432933643, 0.839591685425587, 
                     0.841215448331989, 0.84283166486968, 0.844440278864124, 0.846041234760886, 
                     0.847634477636162, 0.849219953207282, 0.850797607844881, 0.852367388582385, 
                     0.853929243127316, 0.855483119871763, 0.857028967903429, 0.858566737015336, 
                     0.860096377717062, 0.861617841244557, 0.863131079570552, 0.864636045414574, 
                     0.866132692253169, 0.867620974331152, 0.869100846667809, 0.870572265071487, 
                     0.872035186145084, 0.873489567298754, 0.874935366756863, 0.87637254356889, 
                     0.877801057617996, 0.879220869629812, 0.8806319411832, 0.882034234714961, 
                     0.883427713533749, 0.884812341823933), lnd = c(0, 1, 2, 3, 4, 
                                                                    5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 
                                                                    22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 
                                                                    38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 
                                                                    54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
                                                                    70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 
                                                                    86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100)), row.names = c(NA, 
                                                                                                                                                 -101L), class = c("data.table", "data.frame"))

1 个答案:

答案 0 :(得分:1)

首先,我自由地重组了数据框以简化绘图代码:

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)))

然后是情节本身:

尽管使用warning(),您实际上可以为angle的{​​{1}}子参数提供一个整数向量。要将“成角度的” y轴标签对准刻度,我使用了axis.text

hjust = 0.5

enter image description here

我知道解决方案有点混乱,但是它可以工作。