YannG
YannG

Reputation: 11

R geom_text with dodged barplot with xaxis date

I try to add text as in Position geom_text on dodged barplot But it doesn't work with a xaxis Date within ggplot:

Data<-as.data.frame(             
  cbind( 
    as.POSIXct(c("2017-06-26", "2017-06-26" ,"2017-06-26" ,"2017-07-14", "2017-07-14",
    "2017-07-14", "2017-07-14" ,"2017-07-14", "2017-09-14", "2017-09-14"),origin = "1970-01-01", tz = "GMT"),
  c("CHLOROPHYTA","CYANOBACTERIA","INDETERMINES","BACILLARIOPHYTA","CHLOROPHYTA", "CYANOBACTERIA" ,   
    "HAPTOPHYTA","HETEROKONTOPHYTA","CHLOROPHYTA","CYANOBACTERIA" ),
  c(29,8637,3233,97,2816, 63721,282, 18,2001,76593)))
  colnames(Data)<-c("Date","Embranchement","NbCel")

  Data$Date<-as.POSIXct(as.numeric(as.character(Data$Date)), origin = "1970-01-01", tz = "GMT")

  my_plot<-
    ggplot(data=Data, aes(x=Date, y=NbCel, fill=Embranchement,width=1150000)) +
    geom_bar(position = "dodge", stat="identity")+
    geom_text(aes(label=NbCel), vjust=1.6, color="black",
              position = position_dodge(0.9), size=3.5)+
    scale_x_discrete(labels = Data$Date) +
    scale_fill_brewer(palette="Paired")#+
  #axis(1,at=Data$Date,labels = Data$Date)
  print(my_plot)

What I get is :

enter image description here

How do I add text in the middle of each bar?

One additional question is how do I add each x thick mark for each date ?

Thanks!

Upvotes: 1

Views: 1365

Answers (2)

KoenV
KoenV

Reputation: 4283

I am not completely sure what you want to achieve in detail, but here are my 2 cents. You could use the following code:

Your first problem is that NbCel is a factor and is not numeric. You can easily see that on the Y-axis on your graph.

# convert NbCel from factor to integer 
Data$NbCel <- as.integer(as.character(Data$NbCel))

Having fixed this, we work with the graph itself.

The X-axis is a mixture of "date" information an "embranchement". Maybe it is better to separate out those two in different panels (facets) using facet_wrap.

# use a facet-wrap (separate plots in one graph)

ggplot(data=Data, aes(x=Embranchement, y=NbCel, fill=Embranchement)) +
  geom_bar(position = position_dodge(), stat="identity")+
  facet_wrap(~Date ,  ncol = 3)   +         ### plot per date
  scale_fill_brewer(palette="Paired") +
  geom_text(aes(label=NbCel, group = Embranchement), vjust=-.5, color="black",
            position = position_dodge(width = 10), size=2.5) + 
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 6))  # make X labels readible

enter image description here

Please let me know whether this is what you want.

ADDITION

Again, you want to combine different bars from "embranchement" and different "dates" on the X-axis. A more natural thing to do in this case might be a line graph, this way you can superimpose frequency information concerning different data points on 1 date.

Your dataset is not full, i.e. not every date contains all "embranchements".

# indicates measurement not available on every date, hence no lines
with(Data, table(Embranchement, Date))
Embranchement      2017-06-26 2017-07-14 2017-09-14
  BACILLARIOPHYTA           0          1          0
  CHLOROPHYTA               1          1          1
  CYANOBACTERIA             1          1          1
  HAPTOPHYTA                0          1          0
  HETEROKONTOPHYTA          0          1          0
  INDETERMINES              1          0          0

Assuming that absent readings are null-readings, I filled up the dataset, yielding Data2:

Data2 <- rbind(Data, 
               data.frame(Date = as.Date("2017-06-26", format="%Y-%m-%d"), Embranchement = "BACILLARIOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-06-26", format="%Y-%m-%d"), Embranchement = "HAPTOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-06-26", format="%Y-%m-%d"), Embranchement = "HETEROKONTOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-07-14", format="%Y-%m-%d"), Embranchement = "INDETERMINES", NbCel = 0),
               data.frame(Date = as.Date("2017-09-14", format="%Y-%m-%d"), Embranchement = "BACILLARIOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-09-14", format="%Y-%m-%d"), Embranchement = "HAPTOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-09-14", format="%Y-%m-%d"), Embranchement = "HETEROKONTOPHYTA", NbCel = 0),
               data.frame(Date = as.Date("2017-09-14", format="%Y-%m-%d"), Embranchement = "INDETERMINES", NbCel = 0)
               )

Now we may create a line graph:

ggplot(data=Data2, aes(x=Date, y=NbCel, col=Embranchement, linetype =Embranchement) ) +
  geom_line( size=1.1  ) +
  scale_fill_brewer(palette="Paired") +
  geom_text(aes(label=NbCel), vjust=-.75, color="black", size=2.5) 

Yielding the following graph, with "date" on X-axis and a line per "Embranchement".

enter image description here

Even if this isn't what you want, I hope it helps you.

ADDITION 2 (recap of facets but better)

(this answer needs editing, later ;-)

Try this code to get clean numbers on the bars in the facets. The most important change is in geom_text: position_dodge

ggplot(data=Data, aes(x=Embranchement, y=NbCel, fill=Embranchement)) +
  geom_bar(position = position_dodge(), stat="identity")+
  facet_wrap(~Date ,  ncol = 3)   +         ### plot per date
  scale_fill_brewer(palette="Paired") +
  theme_light() +
  geom_text(aes(label=NbCel, group = Embranchement), vjust=-.5, color="black",
            position = position_dodge(width = 1), size=2.5) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 6)) 

You will get this graph:

enter image description here

I think this is closer to what you want. Let me know.

Upvotes: 1

akash87
akash87

Reputation: 3994

Your first problem is that NbCel is a factor and is not numeric. You can easily see that on the Y-axis on your graph.

# convert NbCel from factor to integer 
Data$NbCel <- as.integer(as.character(Data$NbCel))

Now after using some of the code from @KoenV, we now can

ggplot(data=Data, aes(x=as.character(Date), y=NbCel)) +
       geom_bar(aes(fill=Embranchement), position = position_dodge(), stat="identity") +
       scale_fill_brewer(palette="Paired") +
       geom_text(aes(label=NbCel, y = NbCel + 1200, group = Embranchement), color="black", 
                 position = position_dodge(0.9), size=3.5) 

Upvotes: 1

Related Questions