如何更改 ggplot 中构面标签的文本

问题描述 投票:0回答:1

我想更改刻面标签的外观,以便将它们标记为“拉伸强度”和“断裂应变”,而不是显示为“。”之间和小写。我不确定是否需要修改数据中的列名称,或者是否有更好的方法。刚开始使用 R,任何帮助将不胜感激!

##changing titles - didn't appear to do anything

tensile.strength.labs <- c("Tensile Strength")
names(tensile.strength.labs) <- c("Tensile Strength")

##Tried to modify data - didn't appear to do anything

df <- data

df$tensile.strength <- factor(df$tensile.strength,
labels = c("Tensile Strength"))

df$strain.at.break <- factor(df$strain.at.break,
labels = c("Strain at break"))

a <- ggplot(data=data,aes(x=sample,y=c(tensile.strength,strain.at.break)))+
geom_boxplot()+
theme_bw()

data$K.bar <-as.factor(data$K.bar)

ggboxplot(data,x="sample",y=c("tensile.strength","strain.at.break"),
fill="K.bar",combine=TRUE,
xlab = "Sample",
ylab = "", yaxt = "n")

##removes y axis

enter image description here

r ggplot2 label facet-grid
1个回答
0
投票

您可以在数据或图中修改它。下面是图中的示例,您可以在其中使用

str_to_title
str_to_sentence

(如果它们是数据中的列标题,那么我会在图中这样做以避免非语法变量名称。)

library(tidyverse)

df <- tibble(x = 1:2, y = 2:3, facet = c("tensile.strength", "strain.at.break"))

df |> ggplot(aes(x, y)) +
  facet_wrap(~ str_to_title(str_replace_all(facet, "\\.", " ")))

创建于 2024-04-23,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.