Say I have a ggplot, with a continuous variable allocated to the y-axis and a geom_point layer. How would I add the y-axis-value corresponding to each point to the ggplot as an additional layer?
EDIT:
For clarification: Next to each point, I'd like to see a number. That number should be the value of the y-variable that corresponds to the respective point.
CodePudding user response:
This looks weird but seems to be in line with your question
### Library
library(ggplot2)
### Initiating data
set.seed(2)
df <- data.frame(y=rnorm(10),
x=rnorm(10))
### Display plot
ggplot(df, aes(x, y))
geom_point()
scale_y_continuous(sec.axis=sec_axis(~.,
breaks=round(df$y, 2),
labels=round(df$y, 2), name="Additional y axis"))
EDIT
Based on your edit, please find the code to put y values next to each dot.
### Library
library(ggplot2)
### Initiating data
set.seed(2)
df <- data.frame(y=rnorm(10),
x=rnorm(10))
### Display plot
ggplot(df, aes(x, y))
geom_point()
geom_text(x=df$x 0.1, y=df$y, label=round(df$y, 2))