Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to visualize line plot with ggplot in R

Tags:

r

ggplot2

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!

like image 462
김태환 Avatar asked May 20 '26 10:05

김태환


2 Answers

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

like image 196
ziggystar Avatar answered May 22 '26 06:05

ziggystar


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

like image 28
Waldi Avatar answered May 22 '26 04:05

Waldi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!