Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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 - Fatal编程技术网

R 为满足条件的特定条设置颜色

R 为满足条件的特定条设置颜色,r,ggplot2,R,Ggplot2,我在R中有以下数据帧 SalesWeek GS IS MM MN MN 1 2 0.00 742340 3551557.4 384585.0 12044.64 2 3 0.00 0 1770886.0 685880.0 0.00 3 4 0.00 0 2720725.4

我在R中有以下数据帧

  SalesWeek         GS      IS         MM         MN        MN 
1          2       0.00  742340  3551557.4   384585.0   12044.64
2          3       0.00       0  1770886.0   685880.0       0.00
3          4       0.00       0  2720725.4   399797.9  391244.64
4          5       0.00       0 12301207.0   663900.0  122696.43
5          6       0.00       0  8973689.9  3031950.9   12044.64
6          7       0.00       0   264528.3        0.0  314984.64
7          8       0.00       0 11223543.6  1586324.0       0.00
8          9       0.00       0 18721800.9        0.0       0.00
9         10  309085.84       0 12494273.8  7893507.5       0.00
数据框WeeKarea还有其他列,如NL、VS和SL。设置数据框后,我将它们绘制在条形图中

    ggplot(weekarea, aes(x=SalesWeek, y=NL))+
  geom_bar(stat="identity")+
  scale_y_continuous(labels=scales::comma)+ 
  geom_vline(xintercept=12, color="red")+
  xlab("Weeks of the year")+
  ylab("Revenue in Pesos")
我想给第11周到第22周之间的条形图涂上颜色,所以我想修改上面的第一行,如下所示:

ggplot(weekarea, aes(x=SalesWeek, y=NL, fill=(SalesWeek>=12 && SalesWeek<22)))

问题是它把所有的条都涂上了颜色。有人能帮我正确设置吗?

您可以尝试以下方法:

library(ggplot2)

weekarea$pfill <- as.factor((weekarea$SalesWeek>=3 & weekarea$SalesWeek < 5)*1) ##Make a variable basis the conditional logic

###Use the pfill variable in the aes asthetics in ggplot2
ggplot(weekarea, aes(x=SalesWeek, y=MM,fill=pfill))+
  geom_bar(stat="identity")+
  scale_y_continuous(labels=scales::comma)+ 
  geom_vline(xintercept=12, color="red")+
  xlab("Weeks of the year")+
  ylab("Revenue in Pesos")
在这里,我们可以看到第3周和第4周的颜色不同


一种简单的方法是在数据框中添加一个额外的列,指定条形图的颜色,如本例所示:

library(ggplot2)
weekarea<-read.table(text="SalesWeek         GS      IS         MM         MN        MN 
    2       0.00  742340  3551557.4   384585.0   12044.64
    3       0.00       0  1770886.0   685880.0       0.00
    4       0.00       0  2720725.4   399797.9  391244.64
    5       0.00       0 12301207.0   663900.0  122696.43
    6       0.00       0  8973689.9  3031950.9   12044.64
    7       0.00       0   264528.3        0.0  314984.64
    8       0.00       0 11223543.6  1586324.0       0.00
    9       0.00       0 18721800.9        0.0       0.00
    10  309085.84       0 12494273.8  7893507.5       0.00", header=TRUE)

#add color specification:
weekarea$color=ifelse(weekarea$SalesWeek>5 & weekarea$SalesWeek<10, "red", "blue")

#add fill to aes
ggplot(weekarea, aes(x=SalesWeek, y=MM, fill= color))+
  geom_bar(stat="identity")+
  scale_y_continuous(labels=scales::comma)+ 
  geom_vline(xintercept=12, color="red")+
  xlab("Weeks of the year")+
  ylab("Revenue in Pesos")

我试过了,但是pfill列都是0。我认为这意味着一切都是假的。@Angelo Louise Lopez在创建pfill@kpradeep天哪,你是个救生员!我现在犯了错误!