Reduardo
Reduardo

Reputation: 21

Combining bar and line chart with numerical and categorical variables with ggplot R

I'm trying to combine a bar plot and line plot in ggplot using continuous and categorical variables. I basically need to do something like this:

enter image description here

So far I've got this code:

ggplot(data=nutrients, aes(x=Plot, y=Level, fill=Factor)) +
         geom_bar(stat="identity", position = 'dodge', colour="black")

enter image description here

Upvotes: 2

Views: 849

Answers (1)

d.b
d.b

Reputation: 32548

ggplot(mtcars, aes(x = factor(cyl), y = wt, fill = factor(gear))) + 
    geom_bar(stat = "identity", position = "dodge") +
    geom_line(aes(y = ave(wt, cyl, FUN = max), group = gear)) + 
    geom_point(aes(y = ave(wt, cyl, FUN = max), group = gear))

Change the FUN as necessary

enter image description here

Upvotes: 1

Related Questions