0

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:

enter image description here

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. I have tried a number of other solutions to similar problems on SO but nothing is working. 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)
7
  • 5
    Could you add code used to generate your barplots? Commented Jul 3, 2014 at 12:43
  • 1
    geom = "point" would be a good solution for you Commented Jul 3, 2014 at 12:47
  • When you did that bar graph - did you aggregate the data first and added the y value as a variable i.e. stat="identity" rather than stat="bin"? Commented Jul 3, 2014 at 14:38
  • @zx8754 qplot(x = dist.to.forest, y =number.of.species, data = results, facets = .~year, geom = "bar", stat = "identity") Commented Jul 3, 2014 at 20:13
  • 2
    You should also post a sample of your data. A nice way to do it is to export the sample in csv file and post the csv on gist Commented Jul 4, 2014 at 7:31

1 Answer 1

1

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..

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..

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.