Reputation: 57
I have a problem making a graph in R. I have the following data, with flower type, and a color index for different points (distance_petal).
flower_type<-c(rep("blue",3),rep("red",3))
distance_petal1<-c(2,3,2,7,6,7)
distance_petal2<-c(2,2,1,8,6,8)
distance_petal3<-c(1,1,2,9,9,8)
data<-as.data.frame(cbind(flower_type,distance_petal1,distance_petal2,distance_petal3))
data$flower_type<-as.factor(flower_type)
I am trying to make a graph showing distance_petal1, distance_petal2 and distance_petal3 in the X-axis, and the value in the Y-axis. So I want to obtain 2 lines, one for each flower type with 3 points in the Y-axis.
I mean, I want to make something like this, but instead of plotting all values, just plotting the mean for each variable for each factor.
library(GGally)
ggparcoord(data,
columns = 2:4, groupColumn = 1,scale="globalminmax"
)
Does anyone know how to do this?
Thank you very much in advance, have a nice day!
Upvotes: 2
Views: 45
Reputation: 12699
Using ggplot
, dplyr
and tidyr
you can try:
Not sure if your question asks for a line as in the example from GGally or if you want points as in the question. So have included both the line and point version you can just remove the line of ggplot code to get what you need.
library(dplyr)
library(tidyr)
library(ggplot2)
data %>%
pivot_longer(-1) %>%
mutate(value = as.numeric(value))%>%
group_by(flower_type, name) %>%
summarise(mean = mean(value)) %>%
ggplot(aes(name, mean, colour = flower_type))+
geom_line(aes(group = flower_type))+
geom_point()
Created on 2021-04-25 by the reprex package (v2.0.0)
Upvotes: 1