使用ggplot2在R中更改或修改x轴刻度标签

50

我该如何在ggplot2中更改x轴标签的名称?请参见下文:

ggbox <- ggplot(buffer, aes(SampledLUL, SOC)) + geom_boxplot()

ggbox <- ggbox + theme(axis.text.x=element_text(color = "black", size=11, angle=30, vjust=.8, hjust=0.8)) 

ggbox<- ggbox + labs(title = "Land cover Classes") + ylab("SOC (g C/m2/yr)") + xlab("Land cover classes")

上面的代码创建了以下图形: enter image description here

我想要大写这些类的第一个字母(例如Crop,而不是crop)。

我已经尝试了下面的代码,但不确定应该放在哪里以及使用什么函数。 labels = c("Citrus", "Crop", "Cypress Swamp", ..........)

(我正在使用Windows 7和Rstudio)


3
请注意,str_to_sentence 可用于生成所需的大写格式。 https://stringr.tidyverse.org/reference/case.html - flies
1个回答

86

创建标签:

 SoilSciGuylabs <- c("Citrus", "Crop", "Cypress Swamp")

接着添加:

+ scale_x_discrete(labels= SoilSciGuylabs)

10
我使用这个方法将连续变量伪装成分类变量,以便可以使用geom_line。为了使标签出现,我需要先设置断点。我使用了scale_x_continuous(breaks=seq(1,12,1),labels=my_labels)。只是在这里注明,以防对其他人有帮助。 - Alison Bennett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接