简体   繁体   English

如何使用ggplot2在R中创建点图

[英]How to create a dotplot in R using ggplot2

I have data that looks like this 我有看起来像这样的数据

year    species       number.of.seed    dist.to.forest
2006                        0              -5
2006    Bridelia_speciosa   3              0
2006                        0              5
2006    Carapa              5              10
2006                        0              15

And I have created a bar chart, that shows for each year the number of different species found in seed traps and as shown by their distance from forest, which looks like this: 我创建了一个条形图,该条形图每年显示在种子陷阱中发现的不同物种的数量,并显示它们与森林的距离,如下所示:

在此处输入图片说明

bu I would like to use the geom = "dotplot", and have a single dot representing each species which I have counted, basically exactly the same as the bar chart, but instead of the first bar in year 2006, 24 dots, and instead of the second bar 23 dots etc. But when I use geom = "dotplot" I just cant get it to work, not the way i want it, i can get it with a single dot at 24, or 23, but not 24 dots. 我想使用geom =“ dotplot”,并用一个点代表我所计算的每个物种,基本上与条形图完全相同,但不是2006年的第一个条,而是24个点,而是第二个小节的23个点等。但是当我使用geom =“ dotplot”时,我无法使它工作,而不是我想要的方式,我可以在24或23处使用单个点,但不能使用24个点。 I have tried a number of other solutions to similar problems on SO but nothing is working. 对于SO上的类似问题,我已经尝试了许多其他解决方案,但是没有任何效果。 Many thanks in advance. 提前谢谢了。

my code: 我的代码:

dat1<-read.csv(file="clean_06_14_inc_dist1.csv")

diversity<-function(years){    
    results<-data.frame()
    dat2<-subset(dat1, dat1$year %in% years)    
    for (j in years){
        for (i in seq(-5, 50, 5)){
            dat3<-subset(dat2, dat2$year == j & dat2$dist.to.forest == i)
            a<-length(unique(dat3$species))
            new_row<-data.frame(year = j, dist.to.forest = i, number.of.species = a)
            results<-rbind(results, new_row)    
        }}
        print(results)
        qplot(x = dist.to.forest, y =number.of.species, data = results, facets = .~year, geom = "bar", stat = "identity")
}


diversity(2006:2008)

I think your problem is that you are trying to do a dotplot-graph with both an x and y-value as in your bar-graph, whereas I believe dotplot-graphs are meant to be used as histograms, taking only the one variable.. 我认为您的问题是,您试图像在条形图中那样同时使用x和y值来绘制点状图,而我相信点状图只能用作一个直方图,并且仅使用一个变量。 。

So, if I'm not mistaken, if you make your dataframe distinct in the variables you are interested in (since you wanted unique number of species), you can plot it straight away, basically something like 因此,如果我没记错的话,如果您在感兴趣的变量中使数据框与众不同(因为您需要唯一的种类数),则可以立即绘制它,基本上就像

dat2 = unique(dat1[,c("year","species", "dist.to.forest")])

qplot(x=dist.to.forest, data=dat2, facets=.~year, geom="dotplot")

On a side note, I think you may be making this charting more complicated than needs be and you may want to look into dplyr which makes this kind of data manipulation a breeze.. 附带一提,我认为您可能会使这种图表变得比需要的复杂,并且您可能希望研究dplyr ,这使这种数据处理变得轻而易举。

require(dplyr)

dat1 = tbl_df(read.csv(file="clean_06_14_inc_dist1.csv"))

dat2 = dat1 %.%
    filter (year %in% 2006:2008) %.%
    group_by (year, dist.to.forest) %.%
    summarise (number.of.species = n_distinct(species))

qplot(x=dist.to.forest, y=number.of.species, data=dat2, facets=.~year, geom="bar")

Disclaimer: Since you did not provide any sample data, this code is just off the top of my head and may contain errors. 免责声明:由于您未提供任何示例数据,因此这段代码简直就是我的脑海,并且可能包含错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM