![](/img/trans.png)
[英]Plotting more than one measurement per (categorical) group on one single plot in R
[英]plotting all columns of a dataframe in r per grouping variable onto one single plot
D 親愛的,鑒於以下數據框,我正在嘗試繪制每列的值並按類型(有三組,alpha,beta,gamma)整體標記(彩色)。
換句話說,x 軸應顯示 9 個點 (x100,x110.....,x180),y 軸范圍應為 0 到 2(每列的值最大為 0 到 2)。
生成的三行中的每一行都應突出顯示三個分類變量中的每一個。
關於數據幀格式的道歉,我還沒有想出如何漂亮地輸出它。
structure(list(Group.1 = c("alpha", "beta", "gamma"), x100 =
c(1.31729175522923,
0.985278656706214, 0.156200287397951), x110 = c(1.54471416538581,
0.915659603197128, 0.733224726747721), x120 = c(1.27778739808127,
0.813037838321179, 0.779596480540931), x130 = c(1.25000598328188,
0.488610395696014, 0.806707685347646), x140 = c(1.82296009687707,
1.16132276877761, 1.31973652262241), x150 = c(0.929914232343435,
1.41477890312672, 1.41652730805799), x160 = c(1.19612871715799,
0.801679770927876, 0.39746836386621), x170 = c(1.88860023999587,
1.03295020200312, 0.729622524231672), x180 = c(0.926427994389087,
1.20304362708703, 1.57529754098505)), row.names = c(NA, -3L),
class = "data.frame")
我正在嘗試使用 ggplot(任何其他繪圖方法都可以),但是在閱讀 ggplot 函數的要求時,我很難理解是否應該創建一個值向量來用作 aes 參數 y?
提前致謝,
F
要獲得繪圖所需的布局,您需要使用 pivot_longer 將 data.frame 轉換為長格式。 從那里你可以正常使用 ggplot 。 要拆開條形,請使用 position = dodge。
library(tidyverse)
data <- structure(list(Group.1 = c("alpha", "beta", "gamma"),
x100 = c(1.31729175522923, 0.985278656706214, 0.156200287397951),
x110 = c(1.54471416538581, 0.915659603197128, 0.733224726747721),
x120 = c(1.27778739808127, 0.813037838321179, 0.779596480540931),
x130 = c(1.25000598328188, 0.488610395696014, 0.806707685347646),
x140 = c(1.82296009687707, 1.16132276877761, 1.31973652262241),
x150 = c(0.929914232343435, 1.41477890312672, 1.41652730805799),
x160 = c(1.19612871715799, 0.801679770927876, 0.39746836386621),
x170 = c(1.88860023999587, 1.03295020200312, 0.729622524231672),
x180 = c(0.926427994389087, 1.20304362708703, 1.57529754098505)),
row.names = c(NA, -3L),
class = "data.frame")
data %>%
pivot_longer(cols = contains("x"),
names_to = "data_points",
values_to = "vals") %>%
ggplot(aes(x = data_points, y = vals, fill = Group.1)) +
geom_col(position = "dodge")
由reprex 包(v0.3.0) 於 2020 年 11 月 11 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.