如何使用geom_vline和geom_histogram r ggplot2删除填充比例图例中的线 [英] how to remove line from fill scale legend using geom_vline and geom_histogram r ggplot2

查看:1522
本文介绍了如何使用geom_vline和geom_histogram r ggplot2删除填充比例图例中的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基础知识:
使用R统计软件,ggplot2,geom_vline和geom_histogram可视化一些数据。问题在于图例键。



我试图从一些随机模拟中绘制一对直方图,并在该图上添加了几行代表确定性模拟的结果。我已经绘制了数据,但直方图的图例键在它们中间有一条不必要的黑线。你能帮我删除那些黑线吗?一些重现问题的示例代码在这里:

  df1 < -  data.frame(cond = factor(rep(c( A),B),每个= 200)),
rating = c(rnorm(200),rnorm(200,mean = .8)))

df2 < data.frame(x = c(.5,1),cond = factor(c(A,B)))

ggplot(df1,aes(x = rating,fill = cond))+
geom_histogram(binwidth = .5,position =dodge)+
geom_vline(data = df2,aes(xintercept = x,linetype = factor(cond)),
(fill_'stochastic',linetype ='Deterministic')

编辑:添加图片

干杯,
Ryan

解决方案

一种解决方法是更改​​ geom_histogram() geom_vline()。然后添加另一个 geom_vline(),而不是 aes(),只需给出 xintercept = linetype =

  ggplot(data = df1,aes(x = rating)这将不会删除线条,而是将它们隐藏在彩色图例条目下。 ,fill = cond))+ 
geom_vline(data = df2,aes(xintercept = x,linetype = factor(cond)),
show_guide = TRUE)+
geom_histogram(binwidth = .5 ,position =dodge)+
geom_vline(xintercep = df2 $ x,linetype = c(1,3))+
labs(fill ='Stochastic',linetype ='Deterministic')


Basics: Using R statistical software, ggplot2, geom_vline, and geom_histogram to visualize some data. The issue is with the legend keys.

I'm trying to plot a pair of histograms from some stochastic simulations, and on top of that plot a couple of lines representing the result of a deterministic simulation. I've got the data plotted, but the legend keys for the histograms have an unnecessary black line through the middle of them. Can you help me remove those black lines? Some sample code reproducing the issue is here:

df1 <- data.frame(cond = factor( rep(c("A","B"), each=200) ), 
                 rating = c(rnorm(200),rnorm(200, mean=.8)))

df2 <- data.frame(x=c(.5,1),cond=factor(c("A","B")))

ggplot(df1, aes(x=rating, fill=cond)) + 
  geom_histogram(binwidth=.5, position="dodge") +
  geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
             show_guide=TRUE) +
  labs(fill='Stochastic',linetype='Deterministic')

Edit: added image

Cheers, Ryan

解决方案

One workaround is to change the order of geom_histogram() and geom_vline(). Then add another geom_vline() without aes(), just giving xintercept= and linetype=. This will not remove lines but will hide them under the color legend entries.

ggplot(data=df1, aes(x=rating, fill=cond)) + 
  geom_vline(data=df2,aes(xintercept=x,linetype=factor(cond)),
             show_guide=TRUE) +
  geom_histogram(binwidth=.5, position="dodge") +
  geom_vline(xintercep=df2$x,linetype=c(1,3))+
  labs(fill='Stochastic',linetype='Deterministic')

这篇关于如何使用geom_vline和geom_histogram r ggplot2删除填充比例图例中的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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