ggplot2:显示x轴标签的类别和子类别 [英] ggplot2: Show category and sub-category for x-axis labels

查看:125
本文介绍了ggplot2:显示x轴标签的类别和子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

I have a dataset that looks like:

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轴标签下面.

I want the x-axis to be labeled by category and below that I want labels for big_category. For example, I want axis labels for aa,ba, and ca, and then one label below that for big_category a in the ggplot. I dont want it mixed in with the labels for category and I also want it underneath the x-axis labels displayed horizontally.

我也尝试过facet_grid,但这给我带来了一个问题,因为这些条的尺寸不均匀.就像big_category a具有3个类别,而big_category c仅具有1个类别.我希望所有它们都具有相同的宽度,并且我想要一个连续的图.

I've also tried facet_grid but that gives me a problem, as the bars aren't evenly sized. Like big_category a has 3 categories but big_category c has only 1. I want all of them to have the same width, and I want one continuous plot.

更新

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

推荐答案

下面是一个使用示例数据的示例,说明如何在big_category中包含category.为了简单起见,我仅包含了必要的绘图元素.当然,您可以将特定的主题,颜色和其他元素添加到下面的基本图中.

Below is an example, using your sample data, of how to have category within big_category. I've included only the essential plot elements for simplicity. You can, of course, add your specific theme, colors, and other elements to the basic plot below.

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

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

UPDATE: If I understand your comment, here's updated code to left-justify the strip text and remove the strip borders. I don't know of a way (without hacking the underlying graphical objects outside of ggplot) to have just vertical lines between strips. However, I've added back a bit of space between panels and added panel borders to delineate the big_category levels.

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

更新2:我创建了一个具有以下更改的图形:(1)xc标签左端的big_category标签,和(2)空白的big_category category的唯一级别少于3个时标记.为了用空白标签保持相同的切面中断",我们为每个以前的big_category值创建一个唯一的空格字符串(通过改变字符串的长度).

UPDATE 2: I've created a plot with the following changes: (1) big_category labels at the left end of the x-axis labels, and (2) blank big_category labels when category has less than 3 unique levels. To keep the same facet "breaks" with blank labels, we create a unique white-space string (by varying the length of the string) for each former big_category value.

我认为该图看起来不太好(实际上,我认为将big_category刻面条置于其标准位置并以文本居中的方式效果会更好),但是也许您可以尝试使用该图并得到满足您需求的东西.我已经对代码进行了注释,以解释其功能,但是如果有任何不清楚的地方,请告诉我.

I don't think the plot looks very good (I actually think it would work better with the big_category facet strips on top in their standard position and with the text centered), but perhaps you can play around with it and get something that meets your needs. I've commented the code to explain what it's doing, but let me know if anything is unclear.

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

We'll use the new sample_data you posted, but we'll add a fourth level to 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)))

现在,我们将对sample_data进行一些转换,以将其设置为进行绘图并将调整后的数据帧直接传输到ggplot中:

Now we'll make a few transformations of sample_data to set it up for plotting and pipe the adjusted data frame right into ggplot:

sample_data %>% 
  mutate(variable=fct_relevel(variable, c("z","x","y"))) %>% 
  # Create grouping column (called short_cat) to mark levels of big_category 
  #  with two or fewer unique levels of category
  group_by(big_category) %>% 
  mutate(short_cat = length(unique(category)) <= 2) %>% 
  ungroup %>% 
  # Create a unique white-space string for each unique value of grp
  mutate(grp = c(0, cumsum(diff(short_cat) != 0)),
         grp = sapply(grp, function(g) paste(rep(" ", g), collapse="")),
         # Assign white-space strings to each level of big_category for which short_cat 
         # is TRUE
         big_category=replace(big_category, short_cat, grp[short_cat]),
         # Set factor order for big_category so new levels will stay in same order 
         #  as original levels
         big_category=factor(big_category, levels=unique(big_category))) %>%
  ggplot() +
    geom_col(position = "identity", width=0.8, 
             aes(x = category, y = value, fill = variable, color = variable)) + 
    facet_grid(. ~ big_category, space="free_x", scales="free_x", switch="x") +
    theme_bw() +
    theme(axis.text.x=element_text(angle=45, vjust=1, hjust=1),
          strip.placement = "outside",
          strip.background = element_rect(fill=NA,colour=NA),
          panel.spacing.x=unit(0.15,"cm"), 
          # Left justify big_category labels
          strip.text = element_text(hjust=0, face="bold", size=12)) +
    # Expand left side of x-axis so that big_category labels will be under left 
    #  end of x-axis labels
    expand_limits(x=-0.5)

这篇关于ggplot2:显示x轴标签的类别和子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆