Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何更改减淡条形图,使条形图遵循选定的颜色模式,而不是渐变,以及如何防止条形图改变位置?_R_Ggplot2_Bar Chart - Fatal编程技术网

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

R 如何更改减淡条形图,使条形图遵循选定的颜色模式,而不是渐变,以及如何防止条形图改变位置?,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,***一般来说,我对R和编码比较陌生。我在谷歌上搜索了一下,反复尝试,找出了我的错误/解决了我的问题,但都没有用。非常感谢您的帮助 对于上面的文件,我正在创建一个减淡条形图来显示2017-2020年的每个季度。但是,它会产生一个渐变,我需要单独的颜色。例如,我希望所有Q1都是红色的。所有Q2都是绿色的,等等。我试着使用scale\u fill\u manual()但没有成功。 testplot1当你试图给一个你知道是分类变量(四分之一)但被列为数字的变量上色时,这可能会很棘手,因此R解释为一个

***一般来说,我对R和编码比较陌生。我在谷歌上搜索了一下,反复尝试,找出了我的错误/解决了我的问题,但都没有用。非常感谢您的帮助

对于上面的文件,我正在创建一个减淡条形图来显示2017-2020年的每个季度。但是,它会产生一个渐变,我需要单独的颜色。例如,我希望所有Q1都是红色的。所有Q2都是绿色的,等等。我试着使用scale\u fill\u manual()但没有成功。


testplot1当你试图给一个你知道是分类变量(四分之一)但被列为数字的变量上色时,这可能会很棘手,因此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)

当你试图给一个你知道是一个分类变量(四分之一)的变量上色,但它被列为一个数字,因此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)

谢谢你,Shirewoman。这很有帮助。我没想到把填充改为Q而不是销售。慢慢地但肯定地我在学习。太好了!很高兴它有用,@Josh。如果我已经回答了你的问题,请记为已回答。谢谢你,Shirewoman。这很有帮助。我没想到把填充改为Q而不是销售。慢慢地但肯定地我在学习。太好了!很高兴它有用,@Josh。如果我已经回答了你的问题,请将其标记为已回答。
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))