김태환
김태환

Reputation: 45

How to visualize line plot with ggplot in R

I want to make some line plot divided by models.

Here is my dataframe code:

Jumping = c(0.99,0.97,0.99,1,1)
Lunge = c(0.89,0.99,0.99 ,1,1)
Squat = c(0.97,0.99,0.99,1,1)
Stand = c(0.95,0.99, 1,1,1)
Standing_abs = c(1,0.97,0.99,0.99,0.97)
action = c("Jumping","Lunge","Squat","Stand","Standing_abs")
model = c("Knn","Dt","DNN","RF","rbf_SVM")

result = data.frame(Jumping,Lunge,Squat,Stand,Standing_abs,row.names = model)
result

and result >

        Jumping Lunge Squat Stand Standing_abs
Knn        0.29  0.39  0.97  0.65         0.60
Dt         0.97  0.69  0.88  0.99         0.97
DNN        0.99  0.79  0.49  1.00         0.59
RF         1.00  0.77  1.00  0.91         0.39
rbf_SVM    1.00  1.00  1.00  0.58         0.97

But There is some problem. The result I wanted was like..

enter image description here

How can I make line plot like image seperated by models? Have a nice day!

Upvotes: 3

Views: 62

Answers (2)

Waldi
Waldi

Reputation: 41210

You could transpose the data and use pivot_longer to create rows for each model.
Try:

library(tidyverse)
data <- t(result) %>% as.data.frame %>% 
                      rownames_to_column() %>%
                      pivot_longer(cols = rownames(result),names_to = "model")

ggplot(data) + geom_line(aes(group = model, x=rowname,y=value,color=model)) + xlab('Exercice')

enter image description here

Upvotes: 3

ziggystar
ziggystar

Reputation: 28680

require(rtidy)
require(ggplot2)

result %>% 
  add_rownames("model") %>% 
  gather("Activity","value",-model) %>% 
  ggplot(aes(x=Activity,y=value,color=model,group=model)) + geom_line()

enter image description here

Upvotes: 4

Related Questions