ggplot2:显示x轴标签的类别和子类别

时间:2018-01-31 22:43:35

标签: r ggplot2

big_theme <- theme(
  panel.background = element_rect(fill = "black"),
  plot.background = element_rect(fill = "black", colour = NA),
  plot.title = element_text(hjust = 0.5, size = 15, color = "white"),
  axis.text.y = element_text(colour = "white", size = 14),
  axis.title.x = element_text(colour = "white", size = 14),
  axis.title.y = element_text(colour = "white", size = 14),
  axis.text.x = element_text(vjust = 1, angle = 45, color = "white", size = 14, hjust=1),
  strip.background = element_rect(fill = "black"),
  strip.text = element_text(colour = 'white'),
  strip.text.y = element_text(angle = 0), 
  legend.position = "top"
)

theme2 = theme(panel.background = element_rect(fill = 'black'),
  legend.background=element_rect(fill = "black"),
  legend.text = element_text(colour = "white", size = 14), 
  legend.justification = "right",
  legend.key.height = unit(1, "line"),
  legend.key = element_rect(color = "black", fill = "black"))

theme3 = theme(plot.background = element_rect(fill = 'black'))

plot1 <- ggplot(sample_data) + big_theme + theme2 + theme3
plot1 + 
  geom_col(position = "identity", 
           aes(x = category, y = value,
               fill = forcats::fct_relevel(variable, c("z", "x", "y")),
               color = forcats::fct_relevel(variable, c("z", "x", "y")))) + 
  scale_fill_manual(values = c("#000000","#D3667C","#53AC79")) + 
  scale_color_manual(values = c("#00A2EF","#D3667C","#53AC79")) + 
  geom_text(aes(label = big_category, x = big_category, y = 0), vjust = 0, 
            size = 3, color = "white", position = position_dodge(width = 1)) +  
  scale_y_continuous(limits = c(0, 2.4), expand = c(0, 0)) 

我有一个看起来像的数据集:

big_category category   variable    value
a     aa    x   1.2
a     ba    x   1.05
a     ca    x   1.11
a     aa    y   1.43
a     ba    y   1.09
a     ca    y   0.97
a     aa    z   1.12
a     ba    z   1.46
a     ca    z   1.32

b     ab    x   1.2
b     bb    x   1.05
b     cb    x   1.11
b     ab    y   1.43
b     bb    y   1.09
b     cb    y   0.97
b     ab    z   1.12
b     bb    z   1.46
b     cb    z   1.32
c     ac    x   1.2
c     ac    y   1.05
c     ac    z   1.11

我希望x轴按类别标记,而下面我想要big_category的标签。例如,我想要aa,ba和ca的轴标签,然后是ggplot中big_category a的一个标签。我不希望它与类别的标签混合在一起,我也希望它在水平显示的x轴标签下面。

我也尝试过facet_grid,但这给我一个问题,因为酒吧的大小不均匀。像big_category一样,a有3个类别,但big_category c只有1个。我希望它们全部具有相同的宽度,我想要一个连续的情节。

更新

big_category category variable value
a aa111111111 x 1.2
a ba111111111 x 1.05
a ca111111111 x 1.11
a aa111111111 y 1.43
a ba111111111 y 1.09
a ca111111111 y 0.97
a aa111111111 z 1.12
a ba111111111 z 1.46
a ca111111111 z 1.32
b ab111111111 x 1.2
b ab111111111 y 1.05
b ab111111111 z 1.11
c ac111111111 x 1.2
c bc111111111 x 1.05
c cc111111111 x 1.11
c ac111111111 y 1.43
c bc111111111 y 1.09
c cc111111111 y 0.97
c ac111111111 z 1.12
c bc111111111 z 1.46
c cc111111111 z 1.32

代码:

big_theme <- theme(
panel.background = element_rect(fill = "black"),
plot.background = element_rect(fill = "black", colour = NA),
plot.title = element_text(hjust = 0.5, size = 15, color = "white"),
axis.text.y = element_text(colour = "white", size = 14),
axis.title.x = element_text(colour = "white", size = 14),
axis.title.y = element_text(colour = "white", size = 14),
axis.text.x = element_text(vjust = 1, angle = 45, color = "white", size = 14, hjust=1),
strip.background = element_rect(fill = "black"),
strip.text = element_text(colour = 'white'),
strip.text.y = element_text(angle = 0),
legend.position = "top"
)

theme2 = theme(panel.background = element_rect(fill = 'black'),
legend.background=element_rect(fill = "black"),
legend.text = element_text(colour = "white", size = 14),
legend.justification = "right",
legend.key.height = unit(1, "line"),
legend.key = element_rect(color = "black", fill = "black"))

theme3 = theme(plot.background = element_rect(fill = 'black'))

ggplot(sample_data %>% mutate(variable=fct_relevel(variable, c("z","x","y")))) +
geom_col(position = "identity",
aes(x = category, y = value, fill = variable, color = variable)) +
facet_grid(. ~ big_category, space="free_x", scales="free_x", switch="x") +
big_theme + theme2 + theme3 +
theme(strip.placement = "outside",
strip.background = element_rect(fill=NA,colour="white"),
panel.spacing.x=unit(0,"cm"),
strip.text = element_text(hjust=0, face="bold", size=12))

1 个答案:

答案 0 :(得分:4)

下面是一个使用您的示例数据的示例,了解如何在category中拥有big_category。为简单起见,我只包含了必要的情节元素。当然,您可以在下面的基本情节中添加您的特定主题,颜色和其他元素。

library(tidyverse)

ggplot(sample_data %>% mutate(variable=fct_relevel(variable, c("z","x","y")))) +
  geom_col(position = "identity", 
           aes(x = category, y = value, fill = variable, color = variable)) + 
  facet_grid(. ~ big_category, space="free_x", scales="free_x", switch="x") +
  theme_classic() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA, colour="grey50"),
        panel.spacing.x=unit(0,"cm"))

enter image description here

更新:如果我理解您的评论,请在此处更新代码以左对齐条形文本并删除条带边框。我不知道一种方法(没有黑客ggplot之外的底层图形对象)只有条带之间的垂直线。但是,我在面板之间添加了一些空间,并添加了面板边框来描绘big_category级别。

ggplot(sample_data %>% mutate(variable=fct_relevel(variable, c("z","x","y")))) +
  geom_col(position = "identity", 
           aes(x = category, y = value, fill = variable, color = variable)) + 
  facet_grid(. ~ big_category, space="free_x", scales="free_x", switch="x") +
  theme_bw() +
  theme(strip.placement = "outside",
        strip.background = element_rect(fill=NA,colour=NA),
        panel.spacing.x=unit(0.15,"cm"), 
        strip.text = element_text(hjust=0, face="bold", size=12))

enter image description here

更新2 :我创建了一个包含以下更改的图:(1)x轴标签左端的big_category标签,以及(2)空白{ {1}} big_category小于3个唯一级别的标签。为了使相同的facet与空白标签“断开”,我们为每个前category值创建一个唯一的空白字符串(通过改变字符串的长度)。

我不认为情节看起来非常好(我实际上认为它的标准位置和文本居中的big_category小平面条会更好用,但也许你可以玩它可以满足您的需求。我已经对代码进行了评论,以解释它正在做什么,但如果有什么不清楚,请告诉我。

我们会使用您发布的新big_category,但我们会在sample_data添加第四级:

big_category

现在我们将对sample_data = sample_data %>% bind_rows(data_frame(big_category="d", category=c("da1111111111", "db111111"), variable=c("z","x"), value=c(1.1,0.6))) 进行一些转换,将其设置为绘图并将调整好的数据框直接输入ggplot:

sample_data

enter image description here