在ggplot2中更改geom_bar的图例键的形状 [英] Change the shape of legend key for geom_bar in ggplot2

查看:979
本文介绍了在ggplot2中更改geom_bar的图例键的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从geom_bar图形中更改图例关键字的形状。我在网上查看了多个答案,但发现它们在这种情况下不起作用。让我解释一下这个问题:

$ $ $ $ $ $ c $ df1 = data.frame(person = c(person1,person2,person3 ),
variable =variable1,
value = c(0.5,0.3,0.2))

df2 = data.frame(person = c(person1, person2,person3),
variable =variable2,
value = c(-0.3,-0.1,-0.4))
pre>

我试图做一个堆叠的barplot,其中一边是负面的。使用ggplot2我得到:

  library(ggplot2)
ggplot()+ geom_bar(data = df1,aes(x = person,y = value,fill = variable),stat =identity)+
geom_bar(data = df2,aes(x = person,y = value,fill = variable),stat =identity) +
scale_fill_manual(values = c(steelblue,tomato),breaks = c(variable1,variable2),
labels = c(Variable 1,Variable 2 ))

然后看起来像这样:





现在右边的图例默认显示正方形。有没有办法将它变成一个圆圈?

在线我发现这种方式通常使用的方式是使用

  guides(fill = guide_legend(override.aes = list(shape = 1)))

类似的变化。但是,这似乎并不奏效。如果任何人都可以提供帮助,那就太好了,我已经停留了一段时间了。

你可以添加一个图层没有数据的geom_point(仅用于创建图例),并使用 show.legend = FALSE 隐藏栏中不需要的矩形图例:

  df3 = data.frame(person = as.numeric(c(NA,NA)),
variable = c(variable1,variable2 ),
value = as.numeric(c(NA,NA)))

ggplot()+
geom_bar(data = df1,aes(x = person,y =值,fill =变量),stat =identity,show.legend = FALSE)+
geom_bar(data = df2,aes(x = person,y = value,fill = variable),stat =identity ,show.legend = FALSE)+
geom_point(data = df3,aes(x = person,y = value,color = variable),size = 8)+
scale_fill_manual(values = c(steelblue ,tomato),breaks = c(variable1,variable2))+
scale_color_manual(values = c(steelblue,tomato))+
theme(legend.key = element_blank())


I'm trying to change the shape of the legend key from a geom_bar graph. I've looked at multiple answers online but found they didn't work in this case. Let me explain the problem:

df1 = data.frame(person = c("person1", "person2", "person3"),
             variable = "variable1",
             value = c(0.5, 0.3, 0.2))

df2 = data.frame(person = c("person1", "person2", "person3"),
             variable = "variable2",
             value = c(-0.3, -0.1, -0.4))

I'm trying to make a stacked barplot where one side is negative. Using ggplot2 I get:

library(ggplot2)
ggplot() + geom_bar(data = df1, aes(x = person, y = value, fill = variable), stat = "identity") +
  geom_bar(data = df2, aes(x = person, y = value, fill = variable), stat = "identity") +
  scale_fill_manual(values = c("steelblue", "tomato"), breaks = c("variable1","variable2"),
                labels = c("Variable 1", "Variable 2"))

It then looks like this:

Now on the right the legend shows squares by default. Is there a way to change this into a circle for instance?

Online I've found the way this usually works is by using

guides(fill = guide_legend(override.aes = list(shape = 1)))

Or similar variations. However this doesn't seem to work. If anybody can help that would be great, I've been stuck for quite a while now.

解决方案

You could add a layer of geom_point with no data (just to create a legend) and hide the unwanted rectangular legend from the bars using show.legend = FALSE:

df3 = data.frame(person = as.numeric(c(NA, NA)),
                 variable = c("variable1", "variable2"),
                 value = as.numeric(c(NA, NA)))

ggplot() + 
  geom_bar(data = df1, aes(x = person, y = value, fill = variable), stat = "identity", show.legend = FALSE) +
  geom_bar(data = df2, aes(x = person, y = value, fill = variable), stat = "identity", show.legend = FALSE) +
  geom_point(data = df3, aes(x = person, y = value, color = variable), size=8) +
  scale_fill_manual(values = c("steelblue", "tomato"), breaks = c("variable1","variable2")) +
  scale_color_manual(values = c("steelblue", "tomato")) +
  theme(legend.key = element_blank())

这篇关于在ggplot2中更改geom_bar的图例键的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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