相关疑难解决方法(0)

更改 ggplot2 中 geom_bar 的图例键的形状

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

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))
Run Code Online (Sandbox Code Playgroud)

我正在尝试制作一侧为负的堆叠条形图。使用 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 = …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2

5
推荐指数
1
解决办法
2441
查看次数

标签 统计

ggplot2 ×1

plot ×1

r ×1