Plotting a line plot to a scatter plot with ggplot

I have this simple dataframe containing three replications (value) for each factor (CT). I would like to plot it as a geom_point, not as a geom_line point facility.

gene <- c("Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5","Ckap5")
value <- c(0.86443, 0.79032, 0.86517, 0.79782, 0.79439, 0.89221, 0.93071, 0.87170, 0.86488, 0.91133, 0.87202, 0.84028, 0.83242, 0.74016, 0.86656)
CT <- c("ET","ET","ET", "HP","HP","HP","HT","HT","HT", "LT","LT","LT","P","P","P")
df<- cbind(gene,value,CT)
df<- data.frame(df)

      

So, I can make a scatter plot.

ggplot(df, aes(x=CT, y=value)) + geom_point()

      

enter image description here

How to get a geom_line representing the funds for each factor. I tried stat_summary:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
stat_summary(aes(y = value,group = CT), fun.y=mean, colour="red", geom="line")

      

But that won't work. "geom_path: Each group consists of only one observation. Do you need to tweak the group aesthetics?"

But each group has three observations, what's wrong?

Ps. I am also interested in the smooth line.

+3


source to share


2 answers


You must set group aes

to 1:

ggplot(df, aes(x=CT, y=value)) + geom_point() +
  stat_summary(aes(y = value,group=1), fun.y=mean, colour="red", geom="line",group=1)

      



enter image description here

+12


source


You can use the package dplyr

to receive funds for each factor.

library(dplyr)
group_means <- df %>%
  group_by(CT) %>%
  summarise(mean = mean(value))

      

Then you will need to convert the coefficients to numeric so that you can plot lines on the graph using the function geom_segment

. In addition, the function scale_x_continuous

will allow you to set labels for the x-axis.



ggplot(df, aes(x=as.numeric(CT), y=value)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT))

      

Following on from hrbrmstr's comment, you can add a flowing line using the following:

ggplot(df, aes(x=as.numeric(CT), y=value, group=1)) + geom_point() + 
  geom_segment(aes(x=as.numeric(CT)-0.4, xend=as.numeric(CT)+0.4, y=mean, yend=mean), 
               data=group_means, colour="red") +
  scale_x_continuous("name", labels=as.character(df$CT), breaks=as.numeric(df$CT)) + 
  geom_smooth()

      

0


source







All Articles