0
votes

I have this data frame:

d <- data.frame(c(rep("A",5), rep("B",5), rep("C",5), rep("D",5), rep("E",5)),
                sample(c(1:25), 25, replace=TRUE),     
                sample(c(1:25), 25, replace=TRUE))

colnames(d) <- c("col1","col2","col3")

head(d);tail(d)

  col1 col2 col3
1    A   20   13
2    A   24   21
3    A    9   23
4    A   13    1
5    A   14   12
6    B   22    5
   col1 col2 col3
20    D   23    1
21    E    3    1
22    E   24   19
23    E   15    3
24    E   22    6
25    E   21    7

I want to plot separately graphics using GGPLOT. As i have five letters, i will have five line graphics. Each letter of col1 gives me one graphic with col2 and col3 in the same line graphic.

For example:

The first graphic will have x=A and y= col1 and col 3. And the same for the others letters.

How can i do this?

1
would you mind trying something before asking for help? also use dput() to make your dataset available. If you already have a code, please include it in your post.MLavoie
Sorry. Ive edited the question. Thanks.cassius
thanks for adding the dataset. But it's not clear to me what you want. do you want a bar, point, line graph. When you say "each column gives me one graphic" so you want one graphic for col1, one for col2, and one for col3, but the first one is a factor, while the other two are numerical. Please clarify!MLavoie
Sorry, sorry, again. I´ve changedcassius
what kind of line plot do you want to get while the x-axis has only a single value such as "A"?GL_Li

1 Answers

2
votes

ok, your question is still not clear (to me). Is it something like that you would hoping for. You can't have line graph with only one letter. So I created a new column (A1 to A5, etc).

library(ggplot2)
library(gridExtra)

d$V1 <- paste(d$col1,1:5, sep='')
da <- d %>% filter(col1 == "A")
db <- d %>% filter(col1 == "B")
dc <- d %>% filter(col1 == "C")
dd <- d %>% filter(col1 == "D")
de <- d %>% filter(col1 == "E")
#
A <- ggplot(data=da, aes(x=V1, y=col2, group=1)) + geom_point(stat='summary', fun.y=sum, size=0) + stat_summary(fun.y=sum, geom="line") + geom_line(data=da, aes(x=V1, y=col3), color="red") + theme_bw()
B<- ggplot(data=db, aes(x=V1, y=col2, group=1)) + geom_point(stat='summary', fun.y=sum, size=0) + stat_summary(fun.y=sum, geom="line") + geom_line(data=db, aes(x=V1, y=col3), color="red") + theme_bw()
C<- ggplot(data=dc, aes(x=V1, y=col2, group=1)) + geom_point(stat='summary', fun.y=sum, size=0) + stat_summary(fun.y=sum, geom="line") + geom_line(data=dc, aes(x=V1, y=col3), color="red") + theme_bw()
D<- ggplot(data=dd, aes(x=V1, y=col2, group=1)) + geom_point(stat='summary', fun.y=sum, size=0) + stat_summary(fun.y=sum, geom="line") + geom_line(data=dd, aes(x=V1, y=col3), color="red") + theme_bw()
E <- ggplot(data=de, aes(x=V1, y=col2, group=1)) + geom_point(stat='summary', fun.y=sum, size=0) + stat_summary(fun.y=sum, geom="line") + geom_line(data=de, aes(x=V1, y=col3), color="red") + theme_bw()

grid.arrange(A, B, C, D, E, ncol=5)

enter image description here