Let's say I have a dataframe named "df"
A <- seq(1, 10, 2)
B <- c("apple", "strawberry")
combination <- expand.grid(A, B)
colnames(combination) <- c("A", "B")
df <- cbind(combination)
colnames(df) <- c("A", "B")
df$C <- seq(1, 10, 1)
df
What I want to do here is to draw one graph with 2 lines (each line represents "apple" and "strawberry" in column "B"), where x-axis is "A" and y-axis is "C", by using ggplot2. Can anyone help me out to solve this?
Try this. You can enable in aes()
the options color
and group
and set the B
variable. Here the code using ggplot2
functions:
library(ggplot2)
#Data
A <- seq(1, 10, 2)
B <- c("apple", "strawberry")
combination <- expand.grid(A, B)
colnames(combination) <- c("A", "B")
df <- cbind(combination)
colnames(df) <- c("A", "B")
df$C <- seq(1, 10, 1)
df
#Plot
ggplot(df,aes(x=A,y=C,color=B,group=B))+
geom_line()
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.