ggplot2:覆盖密度图R [英] ggplot2: Overlay density plots R

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

问题描述

我想在R中覆盖一些密度图,并且知道有几种方法可以做到这一点,但由于某种原因,它们不适用于我('sm'库不安装,我'米noob足够不了解大部分的代码)。我也试过绘图和标准杆,但我想使用qplot,因为它有更多的配置选项。



我以这种形式保存了数据

  library(ggplot2)
x< - read.csv(剪贴板,sep =\ t,header = FALSE)
x
V1 V2 V3
1 34 23 24
2 32 12 32

我想用V1,V2和V3中的值创建3个覆盖图,使用灰色或灰色填充或使用点线或与图例类似的东西。您可以帮我吗?

谢谢!

解决方案

对于ggplot和多个变量,您需要从宽转换为长格式。我认为这可以做到没有,但这是包的工作方式



这是解决方案,我生成了一些数据(以不同点为中心的3个正态分布)。我也做了一些直方图和箱子图,以防你想要的。 alpha参数控制填充的透明度,如果你使用颜色而不是填充,只能得到轮廓

  x < -  data.frame(v1 = rnorm(100),v2 = rnorm(100,1,1),v3 = rnorm(100,0,2))
library(ggplot2); library(reshape2)$ b $ (data,aes(x = value,fill = variable))+ geom_density(alpha = 0.25)
ggplot(data,aes(x = value,fill =变量))+ geom_histogram(alpha = 0.25)
ggplot(data,aes(x = variable,y = value,fill = variable))+ geom_boxplot()
pre>


I want to overlay a few density plots in R and know that there are a few ways to do that, but they don't work for me for a reason or another ('sm' library doesn't install and i'm noob enough not to understand most of the code). I also tried plot and par but i would like to use qplot since it has more configuration options.

I have data saved in this form

library(ggplot2)
x <- read.csv("clipboard", sep="\t", header=FALSE)
x
     V1     V2    V3
1    34     23    24
2    32     12    32

and I would like to create 3 overlaid plots with the values from V1, V2 and V3 using or tones of grey to fill in or using dotlines or something similar with a legend. Can you guys help me?

Thank you!

解决方案

generally for ggplot and multiple variables you need to convert to long format from wide. I think it can be done without but that is the way the package is meant to work

Here is the solution, I generated some data (3 normal distributions centered around different points). I also did some histograms and boxplots in case you want those. The alpha parameters controls the degree of transparency of the fill, if you use color instead of fill you get only outlines

x <- data.frame(v1=rnorm(100),v2=rnorm(100,1,1),v3=rnorm(100,0,2))
library(ggplot2);library(reshape2)
data<- melt(x)
ggplot(data,aes(x=value, fill=variable)) + geom_density(alpha=0.25)
ggplot(data,aes(x=value, fill=variable)) + geom_histogram(alpha=0.25)
ggplot(data,aes(x=variable, y=value, fill=variable)) + geom_boxplot()

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

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