I would like the following code to show a dot at x = 10, 20 and 30 on subplots 1, 2 and 3 respectively (actually it's y = 10, 20 and 30 but the axes/coordinates are flipped). Instead it is plotting the dot at x = 30 for each one.
pcrtle <- c(10, 20, 30)
df <- data.frame(quartile = c("Q1", "Q2", "Q3", "Q4"), x = c(
1, 1, 1,
1
), y = c(25, 25, 25, 25))
plt1 <- c()
for (ii in 1:length(pcrtle)) {
plt1[[ii]] <- ggplot()
geom_bar(aes(x = x, y = y, fill = quartile), data = df, stat = "identity")
coord_flip()
ylab("")
geom_point(aes(x = 1, y = pcrtle[ii]), size = 5, shape = 21, fill = "#3d3d29")
}
plt2 <- do.call("grid.arrange", c(plt1, ncol = 1))
plt2
The code above produces the following plot
I use the do.call for grid.arrange as the length of Percentile will be variable.
CodePudding user response:
This seems to come about because of non-standard evaluation in ggplot, you can fix it by moving the point's y data outside the aes
because it's not being evaluated in the data dataframe, to give:
geom_point(aes(x = 1), y = pcrtle[ii], size = 5, shape = 21, fill = "#3d3d29")