簡體   English   中英

R ggplot2:使用一列作為 x 軸,其余作為 y 軸

[英]R ggplot2: use one column as x-axis and the rest as y-axis

我有一個類似於以下的大 df

一種 C D
1 10 3 9 5
2 4 6 9 5
4 8 12 7 3

我正在嘗試制作一個散點圖,其中 A 列是 x 值,其余列中的所有值都是 y 值,每列都有不同的顏色。 我嘗試了以下操作,但收到錯誤Error: unexpected ']' in p1

p1 <- ggplot(data = df.p, aes(df.p, aes(x=colnames(df.p[1]), y = colnames(df.p[2:]), colour=df.p[2:]))) + geom_point() + scale_colour_identity()

我也試過x=get(colnames(df.p[1]))x=df.p[1]但我總是得到同樣的錯誤

我在這里嘗試了解決方案但出現錯誤

Error: At least one layer must contain all faceting variables: `variable`.
* Plot is missing `variable`
* Layer 1 is missing `variable`
library(tidyverse)

df <- read.table(header = T, text = " A B   C   D   E
1   10  3   9   5
2   4   6   9   5
4   8   12  7   3")

df %>% 
  pivot_longer(!A) %>%
  ggplot(aes(x = A, y= value, color = name)) +
  geom_point()

reprex 包( v2.0.0 ) 於 2021 年 7 月 15 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM