英文:
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()
# What you want
ggplot(mtcars, aes(mpg, hp, color = factor(vs))) +
geom_point()
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()
# what you want
ggplot(mtcars,aes(mpg,hp,color = factor(vs))) +
geom_point()
<sup>Created on 2023-04-19 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论