首页 > 解决方案 > ggplot2,如何更改条形标签的位置或添加图例

问题描述

我在 ggplot2 中使用 facet_grid 创建了一对条形图。除了条形标签外,我已经根据自己的喜好定制了所有东西。我想出了如何使用 strip.text.y = element_blank() 关闭条形标签,并且可以将标签移动到图表的左侧(开关)。然而,两者都不令人满意。我想将标签放在顶部或制作一个图例。如果我可以让它们到达每个图的顶部,我可能想要删除条带背景并证明文本的位置,比如在每个图的右侧。我找到了对 strip.position 元素的引用,但它对我不起作用。

我尝试了 strip.position 但无法让它工作。事实上,在 R studio 中输入代码时,strip.position 并没有作为选项出现。

创建数据框

cage<-c(3:11)
sage<-c(2:8,10,12:16)
Age<-c(cage,sage)
c<-rep("Choptank",9)
s<-rep("Severn",13)
River<-c(c,s)
n<-c(2,35,19,4,1,52,4,3,2,1,2,39,11,5,2,57,2,1,3,4,2,2)
B<-data.frame(River,Age,n) %>% 
group_by(River,Age) %>% 
tally() %>% 
mutate(prop=n/sum(n))

条形图的主题

bartheme<- theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.spacing.y=unit(1, "lines"),
axis.line = element_line(size = 1,colour = "black"),
axis.title = element_text(colour="black",
size = rel(1.5)),
plot.title = element_text(hjust = 0.5, size=14),
plot.subtitle = element_text(hjust = 0.5,size=14),
panel.background = element_rect(fill="whitesmoke"),
axis.text = element_text(colour = "black",
size = rel(1)))

多面条形图,1 个堆叠在另一个上

Bplot <- ggplot(data = B,
aes(x = Age, y =prop))+ 
geom_bar(stat = 'identity', position = 
position_dodge(),fill="dodgerblue")+
geom_text(aes(label=round(prop,2)), vjust=-0.3, size=3.5)+
facet_grid(rows=vars(River))+
scale_y_continuous(limits=c(0,1), oob=rescale_none,expand=c(0,0))+
scale_x_continuous(breaks=c(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16))

Bplot+bartheme+ggtitle("Age distribution of white 
perch")+ylab("Proportion")

我按预期获得了堆叠图,每个图的右侧都有条形标签(文本垂直)。我更喜欢将条形标签放置在每个图的顶部 - 最好对外观进行一些控制。

标签: rggplot2facet-grid

解决方案


可以更精细地控制构面的布局,facet_wrap您可以使用前缀为strip. 有关详细信息,请参见此处

这是您的数据示例,该示例将标签放在图表顶部并移除阴影,并右对齐文本。我不确定这是否正是您正在寻找的东西,但它应该为您提供根据自己的喜好进行调整的工具。

bartheme <- theme(panel.border = element_blank(),
                  panel.grid.major = element_blank(),
                  panel.grid.minor = element_blank(),
                  panel.spacing.y=unit(1, "lines"),
                  axis.line = element_line(size = 1,colour = "black"),
                  axis.title = element_text(colour="black", size = rel(1.5)),
                  plot.title = element_text(hjust = 0.5, size=14),
                  plot.subtitle = element_text(hjust = 0.5,size=14),
                  panel.background = element_rect(fill="whitesmoke"),
                  axis.text = element_text(colour = "black", size = rel(1)),
                  strip.background = element_blank(),
                  strip.text = element_text(hjust = 1)
)

Bplot <- 
  ggplot(data = B, aes(x = Age, y = prop)) +
    geom_bar(stat = 'identity', position = position_dodge(), fill = "dodgerblue") +
    geom_text(aes(label = round(prop, 2)), vjust = -0.3, size = 3.5) +
    facet_wrap(vars(River), ncol = 1) +
    scale_y_continuous(limits=c(0,1), oob=rescale_none,expand=c(0,0))+
    scale_x_continuous(breaks = c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))

Bplot + bartheme + ggtitle("Age distribution of white perch") + ylab("Proportion")

推荐阅读