ggplot labels

36 views
Skip to first unread message

Kristina Wolf

unread,
Jul 14, 2016, 1:18:30 PM7/14/16
to Davis R Users' Group
Hi all! 

I have created graphs just like this with other datasets, and the labels center over each barplot just fine. In the following code, however, now all the labels stack up all jumbled in the middle of each group of barplots at each depth (depth_bottom_cm). The label is a factor (in the form of a number called system.number, but it is an ordered factor that corresponds to the system.name.number category, which is also ordered). This code works fine with a similar dataset, but not with a new one, but the dataframes were both generated in the same manner. Dataframe attached. Any leads would be much appreciated - such a simple problem, but I have tried all I can think of and web searches have been fruitless thus far. Thank you! 

Code: 

 ggplot(CN_avg, aes(x=depth_bottom_cm , y = mean_C, fill = system.name.number)) + 
  geom_bar(position="dodge", stat = "identity", colour = "black") +   
  geom_errorbar(aes(ymin=mean_C-se_C, ymax=mean_C+se_C), width=.2, position=position_dodge(.9)) +   
  #theme(panel.background = element_blank()) + 
  facet_wrap(~Year, nrow = 4) +
  xlab("Lower Soil Depth") +
  ylab("Carbon (%)") +
  ggtitle("Soil Carbon by System over Time") +
  scale_fill_hue(name="System") + 
  geom_text(aes(label=system.number), position=position_dodge(width=0.9), vjust = -.75, size = 3) +
  theme(plot.title = element_text(face="bold", size=30), 
        axis.title.x = element_text(face="bold", size=20),
        axis.text.x  = element_text(face="bold", size=20),
        axis.title.y = element_text(face="bold", size=20),
        axis.text =element_text(face="bold", size=20), 
        legend.title = element_text(face ="bold", size=20))

CN_avg.csv

Guillaume Théroux Rancourt

unread,
Jul 14, 2016, 1:49:50 PM7/14/16
to davi...@googlegroups.com
Your labels were actually 'dodged', but only subtly. You would need to add a multiplier to the 'width' statement in geom_text and geom_errorbar as seen in the answer at the bottom of the page above.

The updated code below seem to plot what you want. You'll probably want to tweak the label size as many overlap.

 ggplot(CN_avg, aes(x=depth_bottom_cm , y = mean_C, fill = system.name.number)) + 
  geom_bar(position="dodge", stat = "identity", colour = "black") +   
  geom_errorbar(aes(ymin=mean_C-se_C, ymax=mean_C+se_C), width=.2, position=position_dodge(.9*15)) +   
  #theme(panel.background = element_blank()) + 
  facet_wrap(~Year, nrow = 4) +
  xlab("Lower Soil Depth") +
  ylab("Carbon (%)") +
  ggtitle("Soil Carbon by System over Time") +
  scale_fill_hue(name="System") + 
  geom_text(aes(label=system.number), position=position_dodge(width=0.9*15), vjust = -.75, size = 2.5) +
  theme(plot.title = element_text(face="bold", size=30), 
        axis.title.x = element_text(face="bold", size=20),
        axis.text.x  = element_text(face="bold", size=20),
        axis.title.y = element_text(face="bold", size=20),
        axis.text =element_text(face="bold", size=20), 
        legend.title = element_text(face ="bold", size=20))


Cheers,

Guillaume

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Kristina Michelle Wolf

unread,
Jul 14, 2016, 1:57:05 PM7/14/16
to davi...@googlegroups.com

Perfect! I saw that post and tried working it out, but failed to add the adjustment into geom_errorbar()as you did – no wonder it didn’t work. Thank you so much Guillaume!

 

 

Kristina Wolf

Research & Academic Coordinator

Russell Ranch

Agricultural Sustainability Institute

U.C. Davis

Ph.D. Ecology, M.S. Soil Science, B.S. Animal Science

Laboratory (530) 754-5203

Cellular (530) 750-9771

 

green-earth-logo

Reply all
Reply to author
Forward
0 new messages