如何为ggplot2添加手动颜色(geom_smooth / geom_line) [英] How to add manual colors for a ggplot2 (geom_smooth/geom_line)

查看:984
本文介绍了如何为ggplot2添加手动颜色(geom_smooth / geom_line)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2建立一个情节。因此,我使用geom_line来显示直线和geom_smooth,以显示特定索引的Min-Max-Range。
使用两个数据框,第一行由日期(例如:2013-02-04)组成,其次是测量值(例如2.532283)。



首先,我生成一个包含所有样式的空ggplot:
$ b yrange_EVI2 = is指数的范围(最小值 - 最大值)
xrange =是x轴的日期范围(最早 - 最新日期)

  EVI2_veg < -  ggplot()+ geom_blank()+ 
ylim(yrange_EVI2)+ xlim(xrange)+
ggtitle(EVI2 for reference-data在Azraq(约旦))+ ylab(EVI2)+ xlab(月)+
theme_bw(base_size = 12,base_family =Times New Roman)



第二步是绘制范围(最小 - 最大范围)和具有特定值平均值的线:

  EVI2_veg<  -  EVI2_veg + 
geom_smooth(AES(X =日期,Y = Vegetable_mean,YMIN = Vegetable_min,YMAX = Vegetable_max),数据= Grouped_Croptypes_EVI2 ,数据= Sample_EVI2_A_SPOT)

在la第一步骤i试图改变与scale_fill_manual和scale_color_manual的颜色:

  EVI2_veg<  -  EVI2_veg + 
scale_fill_manual( (蔬菜,番茄),值= c(#008B00,#FFFFFF))+
scale_color_manual(最大最大范围和特定作物类型的平均值 Min-Max-Range和Mean \\\
Specific croptypes,labels = c(Vegetable,Tomato),values = c(#008B00,#CD4F39))

我阅读了很多答案和具体包的手册,但我不明白何时使用不同的颜色=和fill =:


  1. geom_line(ads(color =,fill =))

  2. geom_line(ads(),color =,fill =)

  3. scale_color_manual(values = c())或scale_fill_manual =(values = c( ))

如果我没有定义1.没有图例出现。但是,如果我像在代码中那样定义颜色,那么颜色就不符合情节。这是我第一次使用ggplot2,我读了很多这个有用的软件包,但我不明白我如何定义颜色。以及图表和图例中的颜色如何匹配。如果有人能帮助我,那将会很好。

解决方案

首先,使用任何绘图代码包含示例数据总是很好的,否则我们无法运行它来查看您看到的内容。在制作之前,请阅读如何制作一个很好的R可重现的例子其他职位。这会让人们更容易帮助你。无论如何,这里有一些示例数据

  Sample_EVI2_A_SPOT <-data.frame(
Date = seq(as.Date( ),as.date(2014-02-01),by =1 day),
Tomato = cumsum(rnorm(32))

Grouped_Croptypes_EVI2< -data.frame(
Date = seq(as.Date(2014-01-01),as.Date(2014-02-01),by =1 day),
Vegetable_mean = cumsum(RNORM(32))

Grouped_Croptypes_EVI2< -transform(Grouped_Croptypes_EVI2,
Vegetable_max = Vegetable_mean + runif(32)* 5,
Vegetable_min = Vegetable_mean-runif(32)* 5

这会让你想要的情节

  EVI2_veg < -  ggplot()+ geom_blank()+ 
ggtitle(EVI2 for reference-data in Azraq (约旦))+
ylab(EVI2)+ xlab(月)+
theme_bw(base_size = 12,base_family =Times New Roman)+
geom_smooth(aes (x =日期,y = Vegetable_mean,ymin = Vegetable_min,
ymax = Vegetable_max,color =蔬菜,填充=蔬菜),
data = Grouped_Croptypes_EVI2,stat =identity)+
geom_line(aes(x = Date,y = Tomato,color =Tomato),data = Sample_EVI2_A_SPOT)+
scale_fill_manual(name =Min-Max-Range and Mean \\\
of specific Croptypes,
values = c(Vegetable =#008B00,Tomato =#FFFFFF))+
scale_color_manual(name =Min-Max-Range and Mean \\\
of specific Croptypes,
values = c(Vegetable =#008B00,Tomato =#CD4F39))
EVI2_veg

注意添加 color = 和<$ c在 aes()调用中调用$ c> fill = 。你真的应该在 aes()中加入你想要的东西。在这里,我指定了假颜色,然后在 scale _ * _ manual 命令中定义它们。
$ b


I want to built a plot with ggplot2. Therefore i use geom_line to visualize lines and geom_smooth to show the Min-Max-Range of a specific index. Two data frames were used, the first row consists of the date (e.g.: 2013-02-04) and the next are measured values (e.g. 2.532283).

First i generate an empty ggplot with all styles:

yrange_EVI2 = is the Range of the Index (Minimum - Maximum) xrange = is the date range for the x-Axis (earliest - latest date)

EVI2_veg <- ggplot() + geom_blank() + 
            ylim(yrange_EVI2) + xlim(xrange) +
            ggtitle("EVI2 for reference-data in Azraq (Jordan)") + ylab("EVI2") + xlab("month") +
            theme_bw(base_size = 12, base_family = "Times New Roman")

Second step is to plot the Ranges (Min-Max-Range) and lines with the mean for specific values:

EVI2_veg <- EVI2_veg +
            geom_smooth(aes(x=Date, y=Vegetable_mean, ymin=Vegetable_min, ymax=Vegetable_max), data=Grouped_Croptypes_EVI2, stat="identity") +
            geom_line(aes(x=Date, y=Tomato), data=Sample_EVI2_A_SPOT)

In the last step i tried to change the color with scale_fill_manual and scale_color_manual:

EVI2_veg <- EVI2_veg + 
             scale_fill_manual("Min-Max-Range and Mean \nof specific Croptypes",labels=c("Vegetable","Tomato"),values=c("#008B00","#FFFFFF")) +
             scale_color_manual("Min-Max-Range and Mean \nof specific Croptypes",labels=c("Vegetable","Tomato"),values=c("#008B00","#CD4F39"))

I read a lot of answers and the manuals for the specific packages but i don't understand when i use the different colors="" and fill="":

  1. geom_line(ads(color="",fill=""))
  2. geom_line(ads(),color="", fill="")
  3. scale_color_manual(values=c("")) or scale_fill_manual=(values=c(""))

If i don't define the 1. no legend appears. But if i define it like in the Code the color don't match to the plot. Its my first time with ggplot2 and i read a lot of this useful package but i don't understand how i can define the colors. And how the colors from the plot and legend matching. It would be nice if somebody could help me.

解决方案

First, it's always nice to include sample data with any plotting code otherwise we can't run it to see what you see. Please read how to make a great R reproducible example before making other posts. It will make it much easier for people to help you. Anyway, here's some sample data

Sample_EVI2_A_SPOT<-data.frame(
    Date=seq(as.Date("2014-01-01"), as.Date("2014-02-01"), by="1 day"),
    Tomato = cumsum(rnorm(32))
)
Grouped_Croptypes_EVI2<-data.frame(
    Date=seq(as.Date("2014-01-01"), as.Date("2014-02-01"), by="1 day"),
    Vegetable_mean=cumsum(rnorm(32))
)
Grouped_Croptypes_EVI2<-transform(Grouped_Croptypes_EVI2,
    Vegetable_max=Vegetable_mean+runif(32)*5,
    Vegetable_min=Vegetable_mean-runif(32)*5
)

And this should make the plot you want

EVI2_veg <- ggplot() + geom_blank() + 
    ggtitle("EVI2 for reference-data in Azraq (Jordan)") +
    ylab("EVI2") + xlab("month") +
    theme_bw(base_size = 12, base_family = "Times New Roman") + 
    geom_smooth(aes(x=Date, y=Vegetable_mean, ymin=Vegetable_min, 
        ymax=Vegetable_max, color="Vegetable", fill="Vegetable"),
        data=Grouped_Croptypes_EVI2, stat="identity") +
    geom_line(aes(x=Date, y=Tomato, color="Tomato"), data=Sample_EVI2_A_SPOT) +
    scale_fill_manual(name="Min-Max-Range and Mean \nof specific Croptypes",
        values=c(Vegetable="#008B00", Tomato="#FFFFFF")) +
    scale_color_manual(name="Min-Max-Range and Mean \nof specific Croptypes",
        values=c(Vegetable="#008B00",Tomato="#CD4F39"))
EVI2_veg

Note the addition of color= and fill= in the aes() calls. You really should put stuff you want in legends inside aes(). Here i specify "fake" colors that i then define them in the scale_*_manual commands.

这篇关于如何为ggplot2添加手动颜色(geom_smooth / geom_line)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆