单色条形图

时间:2019-05-27 08:26:57

标签: r data-science

我目前已经尝试过一门课程,并想在我的r-studio上复制它,但是,为什么提供下面的代码,我只能显示一个单色条形图?它与填充功能的基本颜色有关吗?谢谢!

head(mtcars)
library(ggplot2)

# Bar chart
ggplot(mtcars, aes(x = cyl, fill = am)) +
  geom_bar(position = "fill")

# Convert bar chart to pie chart
ggplot(mtcars, aes(x = factor(1), fill = am)) +
  geom_bar(position = "fill") +
  facet_grid(. ~ cyl) + # Facets
  coord_polar(theta = "y") + # Coordinates
  theme_void() # theme

1 个答案:

答案 0 :(得分:1)

欢迎您!我认为您只需要将<script src="https://unpkg.com/babel-standalone@6.26.0/babel.js"></script> <script type="text/babel"> //your code </script> 选项添加到group

aes()

enter image description here

但是也许以这种方式它可能更具可读性:

ggplot(mtcars, aes(x = cyl, fill = am, group = am)) + geom_bar(position = "fill")

enter image description here