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

问题描述 投票:-1回答:1

csv file I'm working with

对于上面的文件,我正在创建一个躲避的条形图,以显示2017-2020年的每个季度。但是,它会产生渐变,因此我需要单独的颜色。例如,我希望所有Q1都为红色。所有Q2均为绿色,等等。我尝试使用scale_fill_manual(),但没有运气。gradient result

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个季度的顺序。任何帮助表示赞赏。various colors and rearranged 2020 quarters

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 ggplot2 bar-chart
1个回答
0
投票

当您尝试给一个已知为分类变量(四分之一)但以数字形式列出的变量着色时,这可能会很棘手,因此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)

© www.soinside.com 2019 - 2024. All rights reserved.