Bar Chart-不同颜色的条形图

huangapple go评论66阅读模式
英文:

Bar Chart-Different Colours bars

问题

我尝试创建一个公共数据集Bellabeat的条形图。我的代码如下:

ggplot(data=daily_activity_summary)+
  geom_col(mapping = aes(x=ActivityDateWeekly, y=Average_Total_Steps,fill=ActivityDateWeekly))

问题是每个条形图都有不同深浅的相同颜色(如浅蓝色、深蓝色等)。然而,我希望每个列都有不同的颜色(例如红色、蓝色等)。能有人帮助解决这个问题吗?
谢谢
Panos

我使用了ggplot包,我期望每个条都有不同的颜色(例如红色、蓝色等),但实际显示出来的是不同深浅的相同颜色(例如蓝色)。

英文:

I try to create a bar chart for the public dataset Bellabeat. My code is the following:

ggplot(data=daily_activity_summary)+
  geom_col(mapping = aes(x=ActivityDateWeekly, y=Average_Total_Steps,fill=ActivityDateWeekly))

The issue is that each bar has different shades of the same color (i.e. light blue, dark blue etc). However ,I want each column to have a different color (e.g red ,blue etc). Could someone help with this issue please?
Thanks
Panos

I used ggplot package and I expected each bar has different color (i.e. red,blue etc) but it was shown a bar chart with different shades of the same color (i.e. blue)

答案1

得分: 2

This is because R interprets your variable as continuous and not as a factor. If you specify that it's a factor, then you will get the colors you need. See the example below.

library(tidyverse)

# Not what you want
ggplot(mtcars, aes(mpg, hp, color = vs)) + 
  geom_point()

Bar Chart-不同颜色的条形图


# What you want
ggplot(mtcars, aes(mpg, hp, color = factor(vs))) + 
  geom_point()

Bar Chart-不同颜色的条形图

Created on 2023-04-19 with reprex v2.0.2

英文:

This is because R interprets you variable as continuous and not a factor. If you specify that it's a factor then you will get the colors you need. See the example below.

library(tidyverse)

#not what you want
ggplot(mtcars,aes(mpg,hp,color = vs)) + 
  geom_point()

Bar Chart-不同颜色的条形图


# what you want
ggplot(mtcars,aes(mpg,hp,color = factor(vs))) + 
  geom_point()

Bar Chart-不同颜色的条形图

<sup>Created on 2023-04-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月20日 00:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定