![](/img/trans.png)
[英]Plotting a line for each row in a Dataframe with ggplot2 in R
[英]Plotting dataframe with ggplot2 in R
假設我有一個名為“df”的 dataframe
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
我想在這里做的是用 2 條線繪制一個圖(每條線代表“B”列中的“蘋果”和“草莓”),其中 x 軸是“A”,y 軸是“C”,通過使用 ggplot2。誰能幫我解決這個問題?
試試這個。 您可以在aes()
中啟用選項color
和group
並設置B
變量。 這里的代碼使用ggplot2
函數:
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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.