ggplot2图例着色 [英] ggplot2 legend colouring

查看:112
本文介绍了ggplot2图例着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个5列的文本表。我想在同一个图上绘制4列作为单个密度图。我可以做到这一点如下:


$

  library(ggplot2)
library(grid)
$ p


$ dat< - read.table(textConnection(
file low high high avg lowest
102 4218.0 5437.0 4739.0 4723.0
103 4516.0 5765.0 5061.0 5036.0 $ b $ 104 104 4329.0 5554.0 4858.0 4838.0
107 4094.0 5261.0 4596.0 4578.0
108 4334.0 5569.0 4865.0 4846.0
109 4397.0 5596.0 4924.0 4896.0 $ b $ 110 4046.0 5257.0 4555.0 4547.0
),头=真)

x_low = dat $ low
x_high = dat $ high
x_avg = dat $ avg
x_lowest = dat $最低

绘图仪= ggplot()+ geom_density(aes(x = x_low),color =red,fill =red,alpha = .3, data = data.frame(dat $ low))
plotter = plotter + geom_density(aes(x = x_high),color =blue,fill =blue,alpha = .3,data = data.frame (dat $ high))
plotter = plotter + geom_density(aes(x = x_avg),color =green,fill =green,alpha = .3,data = data.frame(dat $ avg) )
plotter = plotter + geom_density(aes(x = x_lowest),color =purple,fill =purple,alpha = .3,data = data.frame(dat $ minimum))
绘图仪=绘图仪+ xlim(c(2000,7000))
打印(绘图仪)

我现在想在剧情的一边有个传说。根据我的理解,我需要在 aes


$ b括号内移动颜色 $ b

  library(ggplot2)
library(grid)

dat < - read.table(textConnection(
file low high high avg lowest
102 4218.0 5437.0 4739.0 4723.0
103 4516.0 5765.0 5061.0 5036.0 $ b $ 104 4329.0 5554.0 4858.0 4838.0
107 4094.0 5261.0 4596.0 4578.0
108 4334.0 5569.0 4865.0 4846.0
109 4397.0 5596.0 4924.0 4896.0
110 4046.0 5257.0 4555.0 4547.0
),header = TRUE)

x_low = dat $ low
x_high = dat $ high
x_avg = dat $ avg
x_lowest = dat $最低

绘图仪= ggplot ()+ geom_density(aes(x = x_low,color =red,fill =red),alpha = .3,data = data.frame(da t $ low))
plotter = plotter + geom_density(aes(x = x_high,color =blue,fill =blue),alpha = .3,data = data.frame(dat $ high))
plotter = plotter + geom_density(aes(x = x_avg,color =green,fill =green),alpha = .3,data = data.frame(dat $ avg))
plotter = plotter + geom_density(aes(x = x_lowest,color =purple,fill =purple),alpha = .3,data = data.frame(dat $ minimum))

plotter =绘图仪+ xlim(c(2000,7000))
print(绘图仪)





每个图的颜色现在都是错误的(与第一个图相比)以及图例中的标签。



我如何:


  1. 更正着色

  2. 删除每个密度图的黑色轮廓

  3. 更正图例


解决方案

如果你重组组织从 reshape2 包中使用 melt 来定义您的数据。我认为以下代码将为您提供正确的图例,所需的填充颜色,并清除密度图的轮廓:

  dat.m<  -  fusion(dat,id =file)
ggplot(dat.m,aes(value,fill = variable))+ geom_density(alpha = .3,color = NA) + scale_fill_manual(values = c(red,blue,green,purple))


I have a text table with 5 columns. I want to plot 4 of the columns as individual density plots on the same plot. I can achieve this as below:

Code for the above plot:

library(ggplot2)
library(grid)

dat <- read.table(textConnection("
file        low        high       avg               lowest
102         4218.0     5437.0     4739.0            4723.0
103         4516.0     5765.0     5061.0            5036.0
104         4329.0     5554.0     4858.0            4838.0
107         4094.0     5261.0     4596.0            4578.0
108         4334.0     5569.0     4865.0            4846.0
109         4397.0     5596.0     4924.0            4896.0
110         4046.0     5257.0     4555.0            4547.0
"), header=TRUE)

x_low = dat$low
x_high = dat$high
x_avg = dat$avg
x_lowest = dat$lowest

plotter = ggplot() + geom_density(aes(x=x_low), colour="red", fill="red", alpha = .3, data=data.frame(dat$low))
plotter = plotter + geom_density(aes(x=x_high),colour="blue", fill="blue", alpha = .3, data=data.frame(dat$high))
plotter = plotter + geom_density(aes(x=x_avg), colour="green", fill="green", alpha = .3, data=data.frame(dat$avg))
plotter = plotter + geom_density(aes(x=x_lowest), colour="purple", fill="purple", alpha = .3, data=data.frame(dat$lowest))
plotter = plotter + xlim(c(2000,7000))
print(plotter)

I would now like to have a legend on the side of the plot. From my understanding I need to move the colour inside the brackets of aes

I do this as follows:

library(ggplot2)
library(grid)

dat <- read.table(textConnection("
file        low        high       avg               lowest
102         4218.0     5437.0     4739.0            4723.0
103         4516.0     5765.0     5061.0            5036.0
104         4329.0     5554.0     4858.0            4838.0
107         4094.0     5261.0     4596.0            4578.0
108         4334.0     5569.0     4865.0            4846.0
109         4397.0     5596.0     4924.0            4896.0
110         4046.0     5257.0     4555.0            4547.0
"), header=TRUE)

x_low = dat$low
x_high = dat$high
x_avg = dat$avg
x_lowest = dat$lowest

plotter = ggplot() + geom_density(aes(x=x_low, colour="red", fill="red"), alpha = .3, data=data.frame(dat$low))
plotter = plotter + geom_density(aes(x=x_high, colour="blue", fill="blue"), alpha = .3, data=data.frame(dat$high))
plotter = plotter + geom_density(aes(x=x_avg, colour="green", fill="green"), alpha = .3, data=data.frame(dat$avg))
plotter = plotter + geom_density(aes(x=x_lowest, colour="purple", fill="purple"), alpha = .3, data=data.frame(dat$lowest))

plotter = plotter + xlim(c(2000,7000))
print(plotter)

This outputs:

The colours of each plot are now wrong (when compared to the first plot) as well as the labelling in the legend.

How can I:

  1. Correct the colouring
  2. Remove the dark outline of each density plot
  3. Correct the legend

解决方案

You can simplify this if you re-organize your data using melt from the reshape2 package. I think the following code will get you the correct legend, the fill colors you wanted, and get rid of the outline of the density plots:

dat.m <- melt(dat, id="file")
ggplot(dat.m, aes(value, fill=variable)) + geom_density(alpha = .3, color=NA) + scale_fill_manual(values=c("red", "blue", "green", "purple"))

这篇关于ggplot2图例着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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