2

Using this data.frame

DATA

#import_data
df <- read.csv(url("https://www.dropbox.com/s/1fdi26qy4ozs4xq/df_RMSE.csv?raw=1"))

and this script

library(ggplot2)
ggplot(df, aes( measured, simulated, col = indep_cumulative))+
  geom_point()+
  geom_smooth(method ="lm", se = F)+
  facet_grid(drain~scenario)

I got this plot enter image description here

I want to add RMSE for each of the two models (independent and accumulative; two values only) to the top left in each facet.

I tried

geom_text(data = df , aes(measured, simulated, label= RMSE))

It resulted in RMSE values being added to each point in the facets.

I will appreciate any help with adding the two RMSE values only to the top left of each facet.

shiny
  • 3,380
  • 9
  • 42
  • 79

1 Answers1

2

In case you want to plot two numbers per facet you need to do some data preparation to avoid text overlapping.

library(dplyr)
df <- df %>%
  mutate(label_vjust=if_else(indep_cumulative == "accumulative",
                             1, 2.2))

In your question you explicitly told ggplot2 to add label=RMSE at points with x=measured and y=simulated. To add labels at top left corner you could use x=-Inf and y=Inf. So the code will look like this:

ggplot(df, aes(measured, simulated, colour = indep_cumulative)) +
  geom_point() +
  geom_smooth(method ="lm", se = F) +
  geom_text(aes(x=-Inf, y=Inf, label=RMSE, vjust=label_vjust),
            hjust=0) +
  facet_grid(drain~scenario)

enter image description here

echasnovski
  • 1,161
  • 8
  • 13
  • @aelwan `?sprintf` / `?paste` (you'll do better in R if you actually look some things up vs rely on others for quick answers) – hrbrmstr Jan 15 '17 at 09:45
  • @hrbrmstr Thanks for the suggestions – shiny Jan 15 '17 at 09:55
  • @aelwan perhaps lookup other aesthetic parameters for `geom_text()` like text justification and nudging – hrbrmstr Jan 15 '17 at 10:07
  • @evgeniC Thanks. I added RMSE = value ton/ha/yr as below `geom_text(aes(x=-Inf, y=Inf, label= paste("RMSE= ", RMSE, "ton/ha/yr"), vjust=label_vjust, hjust=0))` – shiny Jan 15 '17 at 10:10
  • 1
    @aelwan You're welcome. And yes, that is the way to add customized labels. – echasnovski Jan 15 '17 at 10:15