Reputation: 97
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. 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)
Upvotes: 0
Views: 803
Reputation: 7307
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.
Upvotes: 1