r - Adding legends to multiple line plots with ggplot -


i'm trying add legend plot i've created using ggplot. load data in 2 csv files, each of has 2 columns of 8 rows (not including header).

i construct data frame each file include cumulative total, dataframe has 3 columns of data (bv, bin_count , bin_cumulative), 8 rows in each column , every value integer.

the 2 data sets plotted follows. display fine can't figure out how add legend resulting plot seems ggplot object should have data source i'm not sure how build 1 there multiple columns same name.

library(ggplot2)  i2d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,0,0,2,1,2,2,3), bin_cumulative=cumsum(c(0,0,0,2,1,2,2,3))) i1d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,1,1,2,3,2,0,1), bin_cumulative=cumsum(c(0,1,1,2,3,2,0,1)))   c_data_plot <- ggplot() +    geom_line(data = i1d, aes(x=i1d$bv,  y=i1d$bin_cumulative), size=2, color="turquoise") +   geom_point(data = i1d, aes(x=i1d$bv, y=i1d$bin_cumulative), color="royalblue1", size=3) +    geom_line(data = i2d, aes(x=i2d$bv,  y=i2d$bin_cumulative), size=2, color="tan1") +     geom_point(data = i2d, aes(x=i2d$bv, y=i2d$bin_cumulative), color="royalblue3", size=3) +   scale_x_continuous(name="brightness", breaks=seq(0,8,1)) +   scale_y_continuous(name="count", breaks=seq(0,12,1)) +    ggtitle("combine plot of bv cumulative counts")  c_data_plot 

i'm new r , appreciate help.

per comments, i've edited code reproduce dataset after it's loaded dataframes.

regarding producing single data frames, i'd welcome advice on how achieve - i'm still struggling how data frames work.

first, organize data combining i1d , i2d. i've added column data stores name of original dataset.

restructure data

i1d$data <- 'i1d' i2d$data <- 'i2d' i12d <- rbind.data.frame(i1d, i2d) 

then, create plot, using syntax more common ggplot2:

create plot

ggplot(i12d, aes(x = bv, y = bin_cumulative))+     geom_line(aes(colour = data), size = 2)+     geom_point(colour = 'royalblue', size = 3)+     scale_x_continuous(name="brightness", breaks=seq(0,8,1)) +     scale_y_continuous(name="count", breaks=seq(0,12,1)) +      ggtitle("combine plot of bv cumulative counts")+     theme_bw() 

if specify x , y within ggplot function, not need keep rewriting in various geoms want add plot. after first 3 lines copied , pasted had formatting match expectation. added theme_bw, because think it's more visually appealing. specify colour in aes using variable (data) our data.frame

enter image description here

if want take step further, can use scale_colour_manual function specify colors attributed different values of data column in data.frame i12d:

ggplot(i12d, aes(x = bv, y = bin_cumulative))+     geom_line(aes(colour = data), size = 2)+     geom_point(colour = 'royalblue', size = 3)+     scale_x_continuous(name="brightness", breaks=seq(0,8,1)) +     scale_y_continuous(name="count", breaks=seq(0,12,1)) +      ggtitle("combine plot of bv cumulative counts")+     theme_bw()+     scale_colour_manual(values = c('i1d' = 'turquoise',                                    'i2d' = 'tan1')) 

enter image description here


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -