1

I have two plots that I want to combine into one plot. Plot1 shows average scores for groups within a country, Plot2 shows the country's relative position compared to other countries, as indicated by country-level means. (Plot2 also uses a black point to mark which country is analysed in Plot1 and grey points for the remaining countries, but this is not crucial for my question.)

Plot1 is the important plot, Plot2 helps understand Plot1 by providing a context. The two plots have different x- and y-scales, so I have removed labels for the x-axis for Plot2. Also, I adjust scores on the y-axis in Plot2 (by adding a constant) to avoid overlapping. Actual scores in Plot2 are not important, only the relative position of countries.

In summary, I want Plot1, but with points and the line from Plot2 reproduced within Plot1.

I have found several pages that discuss the integration of two plots into one, but none that solved my problem.

library(ggplot2)

# Within country scores (age-groups)
# -------------------------------------------
age_group<-c("15-19","20-24","25-29","30-34","35-39","40-44","45-49",
         "50-54","55-59","60-64","65-69", "70-74", "75-79", "80-84", "85-89")

scores1<-c(0.000, 0.000, -0.362, -0.546, -0.652, -1.588, -1.333, -1.365, -1.283, -1.394,
       -1.385, -1.622, -1.278, -1.502, -0.909)

within_scores <- data.frame(age_group, scores1)

plot1 <- ggplot(within_scores, aes(age_group, scores1, group = 1)) + geom_point() + 
geom_line() + labs(x = NULL, y = NULL, title="Belgium") + theme_bw() + ylim(-2.2,2) + 
theme(text = element_text(size=8), axis.text.x = element_text(angle=50, hjust=1))

plot1

# Between-country cores (country-level means)
# -------------------------------------------
scores2<-c(-0.429, -0.950, -0.622, 0.000, -0.481, -0.778, -0.657, -1.017, -0.394, -0.546, 
-0.643, -0.844, -0.884, -0.575, -1.079, -0.717, -0.862, -0.481, -0.833, -0.779, -1.577, 
-0.419, -0.255, -0.707, -0.596, -0.283, -1.225, -0.375)


# A constant is added to avoid that the two lines/plots overlap 
scores2 <- scores2 + 1.5 

countries<-c("BE", "BG", "CH", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GR", "HR", 
"HU", "IE", "IL", "LV",  "NL", "NO", "PL", "PT", "RO", "RU", "SE", "SI", "SK", "TR", "UA")
between_scores <- data.frame(countries, scores2)

g1 <- subset(between_scores, countries == "BE")

plot2 <- 
ggplot(between_scores, aes(x=reorder(countries, -scores2), y=scores2, group = 1)) + 
geom_point() + geom_line() + labs(x = NULL, y = NULL, title="Belgium") + 
theme_bw() + ylim(-2,2) + theme(text = element_text(size=8), 
axis.text.x = element_text(angle=50, hjust=1)) + geom_point(colour="grey") + 
geom_line(colour="grey") + geom_point(data=g1, colour="black") + 
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), 
axis.ticks.x=element_blank())

plot2
joran
  • 169,992
  • 32
  • 429
  • 468
cibr
  • 453
  • 5
  • 16
  • Would this work? http://stackoverflow.com/questions/9109156/ggplot-combining-two-plots-from-different-data-frames – Hack-R Dec 13 '16 at 18:32
  • @Hack_R. Thanks. This was one of the pages I saw before posting. The problem in my case is that the two x-scales are different. Error message I receive when trying one of the solutions at the page you refer to: "Error: Aesthetics must be either length 1 or the same as the data (15): x, y, group" – cibr Dec 13 '16 at 18:58
  • You are going to have to make the x-scales fit yourself. ggplot does not know how to map them together and just squishing two plots on top of each other is not going to work. The reason you are getting that error is because ggplot is looking for aesthetics that are not defined in your second dataframe, but are in your global aesthetics call `ggplot(aes(...)`. – Bishops_Guest Dec 13 '16 at 21:57
  • Thanks for the clarification! – cibr Dec 14 '16 at 08:42

0 Answers0