与ggplot2一起工作 [英] working with ggplot2

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

问题描述

我的问题与R(3.2.3)中的ggplot2有关。我有4个数据集包含2列和约80,000-100,000行。我使用下面的代码为一个数据集创建一个图:

  dataset1<  -  read.table(file1.txt ,header = T)
ggplot(data = dataset1,aes(dataset1 $ length))+ geom_histogram(binwidth = 500)+
scale_x_continuous(breaks = seq(300,1000,by = 200), seq(1001,15000,by = 1000))

这里,长度是我数据集的第2列,我想要有这样的情节,x轴显示的长度在300-1200之间,间隔为200(300,500,700,900,1200),长度在1201-1500之间,间隔为1000.因此,我使用上面的代码scale_x_continuous,但它没有产生我想要的。你能帮我用这个阴谋的正确代码吗?



以下是一个简短的数据示例:

 案例长度
C1099757 300
C1099759 300
C1099761 300
C1099763 300
C1100993 301
C1100995 301
C1100997 301
C1100999 301
C1101377 302
C1101379 302
C1101919 303
C1101921 303
C1102979 304
C1102981 304
C1102983 304
C1103475 305
C1103477 305
C1104267 306
C1104269 306

在原始数据文件中,它以我发布的类似方式继续到12000。在你看来,是ggplot2适合这种阴谋,如果不是,请建议正确的。



另外,我正在寻找一种方法来显示所有的长度分布4个数据集合在一个图表中,以便轻松比较它们。

非常感谢您提前付款。

解决方案

我想你应该换成

  scale_x_continuous(breaks = seq(300, 1000,by = 200),seq(1001,15000,by = 1000))

p>

  scale_x_continuous(breaks = c(seq(300,1000,by = 200),seq(1001,15000,by = 1000))) )

  scale_x_continuous(break = seq(300,1000,by = 200)+ 
scale_y_continuous(breaks = seq(1001,15000,by = 1000))



(并不完全确定您的意思)

根据我生成的样本数据一些只包含长度的人造数据

  df1 = data.frame(length = runif(300,300,1200))
df2 = data.frame(length = runif(300,300,1200))
df3 = data.frame(length = runif(300,900,1200))
df2 = data.frame(length = runif(30 0,300,12000))
df4 = data.frame(length = runif(300,300,12000))

#绘制单个数据集
ggplot(data = df4,aes(length ))+ geom_histogram(binwidth = 500)+
+ scale_x_continuous(breaks = c(seq(300,1000,by = 200),seq(1001,15000,by = 1000)))
#combine数据帧
df = data.frame(df1 $ length,df2 $ length,df3 $ length,df4 $ length)
library(reshape)
融化< - melt(df)
ggplot(data = melted,aes(value))+ aes(fill = variable)+ geom_histogram(binwidth = 500)+
scale_x_continuous(breaks = c(seq(300,1000,by = 200) (1001,15000,by = 1000)))



为了有一个更好的X轴标签,我重新分配了一些标签并将它们转过了45度

  ggplot(data = melted,aes(value))+ aes(fill = variable)+ geom_histogram(binwidth = 500,po​​sition =dodge)+ 
scale_x_continuous(breaks = c(seq(300 ,1000,by = 200),seq(1100,15000,by = 1000)))+ theme(axis.text.x = element_text(angle = 45,hjust = 1))



和女巫相应地调整箱子。

  ggplot(data = melted,aes(value))+ 
aes( fill = variable)+
geom_histogram(breaks = c(seq(300,1000,by = 200),seq(1100,15000,by = 1000)),position =dodge)+

scale_x_continuous(breaks = c(seq(300,1000,by = 200),seq(1100,15000,by = 1000)))+
theme(axis.text.x = element_text(angle = 45 ,hjust = 1))


My question is related to ggplot2 in R (3.2.3). I have 4 datasets containing 2 columns and about 80,000-100,000 rows. I used the below code for creating a plot for one the datasets:

dataset1 <- read.table("file1.txt", header=T)
ggplot(data=dataset1, aes(dataset1$length))+ geom_histogram (binwidth =500)+
scale_x_continuous(breaks=seq(300,1000,by=200),seq(1001,15000,by=1000))

Here, length is the 2th column of my dataset, I would like to have the plot, with x-axis shows the length between 300-1200 with interval of 200 (300,500,700,900,1200) and length between 1201-1500 with interval of 1000. So, I used the above code for scale_x_continuous, but it didn't produce what I want. Could you please help me with the correct code for this plotting?

Here is a short sample of data:

case length
C1099757    300
C1099759    300
C1099761    300
C1099763    300
C1100993    301
C1100995    301
C1100997    301
C1100999    301
C1101377    302
C1101379    302
C1101919    303
C1101921    303
C1102979    304
C1102981    304
C1102983    304
C1103475    305
C1103477    305
C1104267    306
C1104269    306

In the original data file, it continues to 12000 in the similar way that I posted. In your opinion, is ggplot2 suitable for this plotting, if not please suggest the right one.

Also, I'm looking for a way to show the length distribution of all 4 datasets in one graph in order to easily compare them. I would be highly appreciated if you could please let me know how I can do that?

Many thanks in advance.

解决方案

I think you should replace

scale_x_continuous(breaks=seq(300,1000,by=200),seq(1001,15000,by=1000))

by

scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000)))

or

scale_x_continuous(breaks=seq(300,1000,by=200)+
scale_y_continuous(breaks=seq(1001,15000,by=1000))

(wasn't entirely sure what you meant)

Based on your sample data i generated some artificial data which only contains length

 df1 = data.frame(length=runif(300,300,1200))
 df2 = data.frame(length=runif(300,300,1200))
 df3 = data.frame(length=runif(300,900,1200))
 df2 = data.frame(length=runif(300,300,12000))
 df4 = data.frame(length=runif(300,300,12000))

# plotting a single dataset
 ggplot(data=df4, aes(length))+ geom_histogram (binwidth =500)+
 +     scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000)))
#combine the datframes
df = data.frame(df1$length,df2$length,df3$length,df4$length)
library(reshape)
melted <- melt(df)
ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500)+
     scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000)))

or

 ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500,position="dodge")+
    scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1001,15000,by=1000)))

To have a slightly nicer X-Axis labelling I redistributed the labels a bit and turned them by 45 degrees

ggplot(data=melted, aes(value))+aes(fill=variable)+ geom_histogram (binwidth =500,position="dodge")+
     scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)))+theme(axis.text.x = element_text(angle = 45, hjust = 1))

And the chart witch adjusts the Bins accordingly. I actually like the different bar sizes.

ggplot(data=melted, aes(value))+
        aes(fill=variable)+ 
        geom_histogram(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)),position="dodge")+

        scale_x_continuous(breaks=c(seq(300,1000,by=200),seq(1100,15000,by=1000)))+
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

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

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