0

Using ggplot2, I would like annotate my faceted geom_pont plots : I am plotting some data per plant for 2 parameters and I would like to annotate each faceted plots with the population size of each plant which make the plot. Below is a similar example to my data. Lets subset the CO2 dataset to make the example more relevant. I count the number of plant for which the uptake is above 20 and rename the column:

require(plyr)
require(dplyr)
require(ggplot2)

CO2_mod<-subset(CO2,uptake>20)
COUNT<-ddply(.data=CO2_mod,
         .variable=.(Plant,Treatment),
         .fun=count)
names(COUNT)[3] <- c("PopSize")

Here is the code for faceted plots based on treatments:

p1<-ggplot(CO2_mod, aes(x=Plant, y=uptake))
p2<-p1+geom_point(aes())+
  facet_grid(Treatment~., scales="free")
p2

Now I would like to annotate each faceted plot with the PopSize value per Plant and per Treatment from the COUNT df. I have tried this code without success:

y<-max(CO2_mod$uptake)+1
COUNT<-mutate(COUNT,y=paste0(y))

p2<-p1+geom_point(aes())+
  facet_grid(Treatment~., scales="free")+
  geom_text(data=COUNT, aes(x=Plant, y=y, label=PopSize), 
        colour="black")
p2

The error warning says : Error: Discrete value supplied to continuous scale

What would be the right way to do this? thanks!

KK_63
  • 105
  • 4

1 Answers1

2

Inspecting COUNT shows that y is a character vector:

str(COUNT)
# 'data.frame': 10 obs. of  4 variables:
#  $ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 2 3 4 5 6 7 8 9 12
#  $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 2 2 2 1 1 1 2
#  $ PopSize  : int  6 6 6 6 6 6 5 6 5 2
#  $ y        : chr  "46.5" "46.5" "46.5" "46.5" ...

If we modify COUNT so that y is numeric:

COUNT<-mutate(COUNT,y=as.numeric(y))

we get this plot: enter image description here

Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48