简体   繁体   中英

Plot line in bar plot in ggplot2

I'm trying to plot a line through the points in my bar plot to view if there is a difference with speed change and observe if there is a trend, however, the line does not appear trying different methods and changing the values within aes as well.

Code Snippet:

File_Size <- c(571, 571, 571, 669, 669, 669, 898, 898, 898, 1700, 1700, 1700, 
               3800, 3800, 3800)

Run_Time <- c(0.786, 0.342, 0.571, 0.856, 0.369, 0.571, 0.890, 0.391, 0.607, 4.10, 1.23, 3.50, 4.25, 1.93, 3.90)

Algorithm <- c("Algo1", "Algo2" ,"Algo3", "Algo1", "Algo2" ,"Algo3","Algo1", "Algo2" ,"Algo3","Algo1", "Algo2" ,"Algo3","Algo1", "Algo2" ,"Algo3")

ggplot(time_complexity, aes(reorder(File_Size, Run_Time), Run_Time, fill = Algorithm)) + 
  geom_bar(stat = 'identity', position = 'dodge') + 
  geom_density(stat = 'identity')

I believe this solves your problem

ggplot(time_complexity, aes(reorder(File_Size, Run_Time), Run_Time, fill = Algorithm)) + 
  geom_bar(stat = 'identity', position = 'dodge', alpha = 0.7) + 
  geom_density(stat = 'identity')+
  geom_line(aes(colour=Algorithm, group=Algorithm),
            size = 2)

you can also select just one line to be ploted:

ggplot(time_complexity, aes(reorder(File_Size, Run_Time), Run_Time, fill = Algorithm)) + 
  geom_bar(stat = 'identity', position = 'dodge', alpha = 0.8) + 
  geom_density(stat = 'identity')+
  geom_line(data = time_complexity[time_complexity$Algorithm == 'Algo1',], 
            aes(reorder(File_Size, Run_Time), Run_Time, colour=Algorithm, group=Algorithm),
            size = 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM