gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:09:26 26 4
gpt4 key购买 nike

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

最佳答案

下面是一个使用示例数据的示例,说明如何在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

更新:如果我理解您的评论,请使用以下更新的代码对带状文本进行左对齐并删除带状边框。我不知道在 strip 之间只有垂直线的方法(无需在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) big_category的唯一级别少于3个时,空白 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)))

现在,我们将对 sample_data进行一些转换,以对其进行设置以进行绘图并将调整后的数据帧直接传输到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)

enter image description here

关于r - ggplot2:显示x轴标签的类别和子类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com