[英]How to plot row by row across multiple columns in R?
假設您的data.frame()
只有數值(這樣我們就不會通過轉換為矩陣而丟失任何信息),我們可以利用apply()
中的名稱自動匹配。
代碼
# Align plots horizontally
par(mfrow = c(1, 2))
# One plot per row
apply(df, 1, function(x){
barplot(x, las = 2)
})
數據
df <- as.data.frame(rbind(rnorm(12), rnorm(12)))
names.m <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
names(df) <- names.m
您可以通過轉動數據框的方向來做到這一點。 例如:
df <- data.frame(a = c(1,2), b = c(2,4))
> df
a b
1 1 2
2 2 4
df <- data.frame(t(df))
df$group <- rownames(df)
> df
X1 X2 group
a 1 2 a
b 2 4 b
## Then you need the library ggplot2
library(ggplot2)
ggplot(df, aes(x = X1, y = X2, color = group)) +
geom_point()
我希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.