繁体   English   中英

如何更改闪避条形图,使条形遵循选定的颜色模式,而不是渐变,以及如何防止条形改变位置?

[英]How do I change a dodged bar graph so the bars follow a selected color pattern,not a gradient, and how to keep bars from changing positions?

我正在使用的 csv 文件

***我对 R 比较陌生,一般来说编码。 我做了一些谷歌搜索和反复试验来找出我的错误/解决我的问题,但无济于事。 非常感谢帮助。

对于上面的文件,我正在创建一个闪避条形图来显示 2017-2020 年的每个季度。 但是,它会产生一个渐变,我想要个人 colors。例如,我希望所有 Q1 都是红色的。 所有 Q2 都是绿色的,等等。我尝试使用 scale_fill_manual() 但没有成功。 梯度结果

testplot1<-ggplot(test, aes(x=Year, y=Sales, fill=Sales))+
  geom_bar(position = "dodge", stat="identity", aes(group=Q))+
  labs(x = "Year", y = "Sales",
       title="Sales Year-Over-Year",
       subtitles = "Test")+
  theme_bw()+
  theme_minimal()+
  theme(axis.ticks = element_blank(),
        panel.grid.minor.x=element_blank(),
        panel.grid.major.x=element_blank(),
        plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))+
  theme(legend.position = "none")+
  geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
            size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
  scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot1

我尝试将填充设置为 as.factor(Sales),但随后它更改了 2020 季度的顺序。 任何帮助表示赞赏。 各种 colors 和重新排列的 2020 季度

testplot<-ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Sales)))+
  geom_bar(position = "dodge", stat="identity", aes(group=Q))+
  labs(x = "Year", y = "Sales",
       title="Sales Year-Over-Year",
       subtitles = "Test")+
  theme_bw()+
  theme_minimal()+
  theme(axis.ticks = element_blank(),
        panel.grid.minor.x=element_blank(),
        panel.grid.major.x=element_blank(),
        plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.line = element_line(colour = "black"))+
  theme(legend.position = "none")+
  geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
            size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
  scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot

当您尝试为您知道是分类变量(四分之一)但列为数字的变量着色时,这可能会很棘手,因此 R 解释为连续变量。 您可以通过将其编码为这样的一个因素来解决这个问题:

ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Q)))+
      geom_bar(position = "dodge", stat="identity") +
      labs(x = "Year", y = "Sales",
           title="Sales Year-Over-Year",
           subtitles = "Test") +
      scale_fill_manual(values = c("red", "green", "blue", "gray")) +
      theme_bw() +
      theme_minimal() +
      theme(axis.ticks = element_blank(),
            panel.grid.minor.x=element_blank(),
            panel.grid.major.x=element_blank(),
            plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
            plot.subtitle = element_text(hjust = 0.5),
            panel.border = element_blank(),
            panel.background = element_blank(),
            axis.line = element_line(colour = "black"))+
      theme(legend.position = "none")+
      geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
                size = 2, position = position_dodge2(width = 1),
                inherit.aes = TRUE) +
      scale_y_continuous(labels=dollar_format(prefix="$"), 
                         expand = c(0,0), limits = c(-200000,800000))

关键位是fill = as.factor(Q)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM