2

I have created a plot using the facet function in R.

The plot that I have created is to show percent frequency on the Y axis and some value on the x axis. I would like to use the facet function because I like the visual appearance. However, when the facet function is used my percent frequency is calculated by using all of the observations no matter the group they are in.

What I want to do is to still use the facet function but have the percent frequencies calculated from just the group.

Here is the code that I have used to create plots so far:

Age <- data.frame(mydata$Age, mydata$Year)
colnames(Age) <- c("Age", "Year")

Age_wk <- Age[complete.cases(Age$Age),] #to remove NA's present
med.fac1 = ddply(Age_wk, .(Year), function(.d) # to create medians within facet groups
data.frame(x=median(.d$Age)))

library(plyr)

library(ggplot2)
library(scales)
total_age <- ggplot(Age_wk, aes(x= Age)) + geom_histogram(aes(y=   (..count..)/sum(..count..)), binwidth=1)+ scale_y_continuous(labels=percent_format(),     name="Frequency (%)")+
     xlab("Age (yrs)") + scale_x_continuous(limits = c(1,30), breaks =seq(1,30,1)) + 
     facet_grid(Year ~.)+ # making facet grid so  catch Year is vertical
     geom_vline(data=med.fac1, aes(xintercept=x), type="dashed")+
     # making vertical medians   with faceted set up
     ggtitle ("Age Frequencies across Catch Years")+
     theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(), # removes both    horizontal and vertical gridlines
           panel.background=element_rect(colour="black", fill="white")) 

Here is a subset of data:

structure(list(Age = c(6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L,     9L), Year = c(2011, 
2011, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013,    2013, 2013, 2013 )), .Names = c("Age", "Year"), row.names = c(NA, 6L), class = "data.frame")

What I want is to calculate age frequencies within years (2011-2013) and not for the entire combined group. It seems that this is happening when I use the facet function. Is there a way to override this? Or maybe this is just what facet_grid does... Thoughts appreciated. Thanks

Elizabeth
  • 199
  • 1
  • 4
  • 13
  • Please read the info about how to produce a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). This will make it much easier for others to help you. – Jaap Jun 09 '14 at 16:22
  • @Jaap- sorry, I knew I was forgetting something :/ Will do that now – Elizabeth Jun 09 '14 at 16:55

1 Answers1

1

You current dataset itself is

Age = structure(list(Age = c(6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L,9L), 
               Year = c(2011, 2011, 2011, 2011, 2011, 2011, 2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013,    2013, 2013, 2013 )), .Names = c("Age", "Year"), 
          row.names = c(1:18), class = "data.frame")

table(Age)
   Year
Age 2011 2012 2013
  5    1    1    1
  6    2    2    2
  7    2    2    2
  9    1    1    1

So when you prepare percentage wise plot they will look same...

I have prepared a dummy data set and used my earlier suggested code

Age_wk = data.frame(Age = sample(5:9,130,replace=TRUE),
                 Year = sample(2010:2014,130,prob=c(0.05,0.3,0.3,0.3,0.05),replace=TRUE))

ggplot(Age_wk[Age_wk$Year %in% 2011:2013,], aes(x= Age)) + geom_histogram(aes(y=   (..count..)/sum(..count..), fill=as.factor(Age)), col = 'black', binwidth=1) + 
  scale_y_continuous(labels=percent_format(), name="Frequency (%)")+
  xlab("Age (yrs)") +  facet_grid(Year ~ ., scales = "free") + # making facet grid so  catch Year is vertical
  geom_vline(data=med.fac1, aes(xintercept=x), type="dashed")+
  # making vertical medians   with faceted set up
  ggtitle ("Age Frequencies across Catch Years")+
  theme(panel.grid.major = element_blank(), panel.grid.minor=element_blank(), # removes both    horizontal and vertical gridlines
        panel.background=element_rect(colour="black", fill="white"))

Output for the same is given below

enter image description here

vrajs5
  • 4,066
  • 1
  • 27
  • 44
  • @vrajs5- year is numeric however this does not seem to accomplish what I am looking for. – Elizabeth Jun 11 '14 at 17:47
  • @Jaap- I've added a small subset of data. I've also read "Normalizing faceted histograms separately in ggplot2" posted back in 2013 by adding the ..ncount option instead of ..count.. but that does not work. Perhaps I should just abandon faceting for this particular purpose? – Elizabeth Jun 11 '14 at 17:50