-2

I have used the code below to produce a graph showing the results from 2 different data sets, shown as two lines on a graph. The code doesn't bring up a legend and nothing I do from any of these forums seems to work. Please could someone take a look? Thanks

taqtable<-ddply(baseline,. (X.step.), summarize,
                mean=mean(TotalAdultQueens),
                sd=sd(TotalAdultQueens),
                se=se(TotalAdultQueens))


taqtableleg<-ddply(legume,.(X.step.),summarize,               
mean=mean(TotalAdultQueens),
                   sd=sd(TotalAdultQueens),
                   se=se(TotalAdultQueens))



ggplot(taqtable,aes(x=X.step.,y=mean))+
  ggtitle("(d)")+
  xlab("Day")+
  ylab("Total Adult B.pascorum Queens (mean)")+
  #xlim()+
  ylim(0,4500)+ 
  geom_line(color="red", size=1)+
  geom_line(data=taqtableleg, aes(x=X.step., y=mean),color="blue", size=1)+
  theme(axis.text.x=element_text(angle=0))
aosmith
  • 34,856
  • 9
  • 84
  • 118
ELD
  • 1
  • Hi ELD, what have you tried so far? – hakamairi Aug 06 '18 at 15:46
  • This looks like it's like a duplicate of https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot . I also wrote about this [here](https://aosmith.rbind.io/2018/07/19/legends-constants-for-aesthetics-in-ggplot2/) since I've been seeing this question a lot this summer. – aosmith Aug 06 '18 at 16:17
  • Possible duplicate of [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – divibisan Aug 06 '18 at 16:25
  • Can't say for sure unless you include [representative samples of your data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but this is the sort of situation where you want to work within `ggplot`'s intended paradigm—that is, long-shaped data with variables, such as legume vs baseline, that you can map to an aesthetic, such as line color. – camille Aug 06 '18 at 16:26
  • 1
    Answer: Join the datasets into one. – Jack Brookes Aug 06 '18 at 16:40
  • dput(taqtableleg) – Aleksandr Aug 06 '18 at 17:49
  • Thank you for all the helpful comments :) managed to work it out – ELD Aug 20 '18 at 16:40

1 Answers1

0

You can add id column to both data.frames then rbind them. Please the code below:

library(plyr)

#data simulation
set.seed(123)
n <- 1000
se <- function(x) {
  sd(x) / sqrt(length(x))
}

baseline <- data.frame(X.step. = (sample(1:20, n, replace = TRUE)),
                       TotalAdultQueens = floor(rnorm(n, mean = 2000, sd = 5000)))

legume <- data.frame(X.step. = (sample(1:20, n, replace = TRUE)),
                     TotalAdultQueens = floor(rnorm(n, mean = 2000, sd = 5000)))

# data transformation
taqtable <- ddply(
  baseline,
  . (X.step.),
  summarize,
  mean = mean(TotalAdultQueens),
  sd = sd(TotalAdultQueens),
  se = se(TotalAdultQueens)
)

taqtable$id = "taqtable"

taqtableleg <- ddply(
  legume,
  .(X.step.),
  summarize,
  mean = mean(TotalAdultQueens),
  sd = sd(TotalAdultQueens),
  se = se(TotalAdultQueens)
)

taqtableleg$id <- "taqtableleg"


result <- rbind(taqtable, taqtableleg)

# plot
ggplot(result, aes(x = X.step., y = mean, color = id)) +
  ggtitle("(d)") +
  xlab("Day") +
  ylab("Total Adult B.pascorum Queens (mean)") +
  ylim(0, 4500) +
  geom_line(size = 1) +
  theme(axis.text.x = element_text(angle = 0))

ggplot2 demo

Artem
  • 3,304
  • 3
  • 18
  • 41