我正在用来ggplot
制作一个三面板人物。我想facet_wrap
按州并在标签中包含城市名称,但是,当我这样做时,州和城市名称之间会出现一条水平线。如何去掉水平线?
library(tidyverse)
set.seed(333)
# Example dataset
df <- data.frame(
state = c("North Carolina","North Carolina","North Carolina",
"North Carolina","North Carolina","North Carolina",
"Virginia","Virginia","Virginia"),
city = c("Boone","Boone","Boone",
"Elizabeth City","Elizabeth City","Elizabeth City",
"Norfolk","Norfolk","Norfolk"),
year = rep(seq(2000,2004,2),3),
value = round(rnorm(9,100,50))
)
# Example plot (how can I remove the horizontal line between the state and city labels?)
df %>%
ggplot(aes(x = year, y = value)) +
geom_point() +
theme_bw() +
facet_wrap(~state+city)
创建于 2024-04-19,使用reprex v2.1.0
如果您希望保留 theme_bw(),一种选择是创建一个新变量,将州和城市连接起来,并用换行符 (\n) 分隔:
然后您可以使用新变量来显示图表:
因此完整的代码将是:
有几种方法可以实现这一目标。更改主题,例如。到
theme_light
(A),或修改条带背景和文本颜色 (B)。这些方法都不会修改数据。