1
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))
data2 <- 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")

tidyr::pivot_longer(data, cols = -partition) %>%
    ggplot() + aes(x = partition, y = value) +
      geom_col(aes(fill = name)) +
      geom_line(data=data2, aes(x=partition, y=output_data, group = 1), colour="blue")

I am plotting a stacked bar and line graph in one figure as shown here: Graph The value for the line graph is in MB while for the stack bar its in milliseconds. The line graph is in blue which is not being differentiated due to the scale of the values. Any help on this is highly appreciated. Thanks

camille
  • 16,432
  • 18
  • 38
  • 60
  • perhaps a secondary y-axis would help. for example: https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales – efz May 21 '20 at 17:29

1 Answers1

0

Well even without throwing in the line geom your plot was suffering from the huge range in values from a low of 2914.202 to a high of 810357.382. Easiest one line solution is simply change your y axis scale to something like log10 (see here for more)

Would look like this...

library(ggplot2)
library(tidyr)

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))
data2 <- data.frame(partition = c("1", "2", "3", "4","5"), 
                    output_data = c(199.1,199.1,199.1,99.5,99.5))


tidyr::pivot_longer(data, cols = -partition) %>%
  ggplot() + aes(x = partition, y = value) +
  geom_col(aes(fill = name)) +
  geom_line(data=data2, aes(x=partition, y=output_data, group = 1), colour="blue") + 
  scale_y_log10()

Chuck P
  • 3,862
  • 3
  • 9
  • 20