如何使用ggplot填充geom_area()图? [英] How do I fill a geom_area() plot using ggplot?

查看:1738
本文介绍了如何使用ggplot填充geom_area()图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的geom_area情节:



我想将x轴上方的所有内容变成绿色,将x轴下方的所有内容变成红色。我的数据中有一个类别列,其中包含所有正值的字符串positive和所有负值的negative字符串,所以我尝试了简单的fill = category并使用scale_fill_manual将正值设置为绿色,将负值设置为红色,但这给了我:



绿色上面的x看起来是正确的,但是轴下面的红色是不正确的。我检查了我的数据,并且没有负面数据点,它在Oct20之后着色为红色,而使用geom_point代替我获得了正确的颜色。



这是我的一个样本数据:

 创建的分数类别
2011-10-19 21:26:19 2正数
2011 -10-19 22:50:33 -2负
2011-10-20 15:12:38 -2负
2011-10-20 17:19:24 -2负
2011-10-20 22:12:44 2正面
2011-10-20 22:16:57 4正面
2011-10-21 08:22:53 2正面

以下是我用来制作图的代码:

ggplot(data = df,aes(x = created,y = score,color = category))+ geom_point(aes(fill = category))+ scale_fill_manual(values = c(positive=绿色,负面=红色))



我的问题可能与 THI

您需要为每个正/负片段创建一个新的分组变量。为了使转换变得更加块状,您可以先插入数据:

  require(ggplot2)

#加载数据
df = read.table('data.txt',header = T)
df $ created = as.POSIXct(df $ created,tz ='UTC')

#插值数据
lin_interp =函数(x,y,length.out = 100){
approx(x,y,xout = seq(min(x),max(x ),length.out = length.out))$ y
}
created.interp = lin_interp(df $ created,df $ created)
created.interp = as.POSIXct(created。 interp,origin ='1970-01-01',tz ='UTC')
score.interp = lin_interp(df $ created,df $ score)
df.interp = data.frame(created = created.interp,score = score.interp)

#为每个pos / neg段创建一个分组变量
cat.rle = rle(df.interp $ score< 0)
df.interp $ group = rep.int(1:长度(cat.rle $长度),times = cat.rle $长度)

#地块
dev.new(宽度= 6,height = 4)
ggplot(data = df.interp,aes(x = created,y = score,fill = score> 0,group = )+ geom_area()+ scale_fill_manual(values = c('green','red'))


I have a geom_area plot that looks like this:

I want to color everything above the x-axis green and everything below the x axis red. I have a category column in my data that contains the string "positive" for all positive values and "negative" for all negative values, so I tried simply doing fill = category and using scale_fill_manual to set positive to green and negative to red, but that gives me this:

Green the above the x looks right, but the red below the axis isn't right. I've checked my data and there are no negative data points where it's coloring red after Oct20, and using geom_point instead I get the correct colors.

Here's a sample of my data:

created                 score   category
2011-10-19 21:26:19     2   positive
2011-10-19 22:50:33    -2   negative
2011-10-20 15:12:38    -2   negative
2011-10-20 17:19:24    -2   negative
2011-10-20 22:12:44     2   positive
2011-10-20 22:16:57     4   positive
2011-10-21 08:22:53     2   positive

and here's the code I'm using to make the plot:

ggplot(data = df, aes(x = created, y = score, colour = category)) + geom_point(aes(fill = category)) + scale_fill_manual(values = c("positive" = "green", "negative" = "red"))

My problem might be related to this previous question.

解决方案

You need to make a new grouping variable for each positive/negative segment. To make the transitions less "blocky", you can just first interpolate the data:

require(ggplot2)

# Load data
df = read.table('data.txt', header=T)
df$created = as.POSIXct(df$created, tz='UTC')

# Interpolate data
lin_interp = function(x, y, length.out=100) {
    approx(x, y, xout=seq(min(x), max(x), length.out=length.out))$y
}
created.interp = lin_interp(df$created, df$created)
created.interp = as.POSIXct(created.interp, origin='1970-01-01', tz='UTC')
score.interp   = lin_interp(df$created, df$score)
df.interp = data.frame(created=created.interp, score=score.interp)

# Make a grouping variable for each pos/neg segment
cat.rle = rle(df.interp$score < 0)
df.interp$group = rep.int(1:length(cat.rle$lengths), times=cat.rle$lengths)

# Plot
dev.new(width=6, height=4)
ggplot(data = df.interp, aes(x = created, y = score, fill=score>0, group=group)) + geom_area() + scale_fill_manual(values = c('green', 'red'))

这篇关于如何使用ggplot填充geom_area()图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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