0
library(ggplot2)
library(reshape2)

data <- data.frame(partition = c("1", "2", "3", "4","5"), 
edge=c(2914.2025,4274.438333,7072.29,7984.68,10232.96333), 
cloud=c(11445.02,10384.94,9165.71,7884.15,7113.79),
communication=c(803345.0248,805614.764,810357.3823,460484.3287,483277.6666))
df2 <- data.frame(partition = c("1", "2", "3", "4","5"),output_data=c(199.1,199.1,199.1,99.5,99.5))
elections_long <- melt(data, id = "partition")
ggplot(elections_long, aes(x = partition, y = value)) +
geom_bar(stat="identity", aes(fill = variable))+geom_line(data=df2, aes(x=partition, y=value), 
colour="blue")

I am facing some issue in plotting a graph in R like a graph represented in below stacked bar graph.

enter image description here

Edge, cloud, and communication are represented in the stacked bar while output data should represented as a line graph.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • By design, in `ggplot2` it is avoided the superposing of two different y scales. You can check [this post](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) for guidance – Crimc May 20 '20 at 02:11
  • So I frequently have to make plots that combine two pieces of information and I have found it best to rescale every piece of information onto a 0-1 scale in increments of 0.1 (and sometimes even 0.05). Then the axis tick marks are handles via the `labels` argument within `scale_x/y_continuous()` and `sec_axis()` respectively using some appropriate values. – statstew May 20 '20 at 04:11

1 Answers1

0

You can get data in long format, create a stacked bar chart and add a line on top of it using data from df2.

library(ggplot2)

tidyr::pivot_longer(data, cols = -partition) %>%
   ggplot() + aes(x = partition, y = value) +
   geom_col(aes(fill = name)) +
   geom_line(data=df2, aes(x=partition, y=output_data, group = 1), colour="blue")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Hi, @Ronak Shah, I ran the above the code but I am getting error 'Error in FUN(X[[i]], ...) : object 'name' not found'. Thank you for your valuable help. –  May 20 '20 at 22:47
  • @emandavis Well, that's weird it doesn't give me any error message for the data posted above. Can you restart R and try again? – Ronak Shah May 20 '20 at 23:56
  • @Ronah Shah Thank you so much it's working now but I have a question related to the scale output data is in MB while others are execution time in seconds. How to differentiate this in the current code? –  May 21 '20 at 13:04
  • Sorry, I don't understand. Probably it is better to ask a new question including the details. – Ronak Shah May 21 '20 at 14:14