简体   繁体   English

R ggplot2:使用一列作为 x 轴,其余作为 y 轴

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

I have a large df resembling the following我有一个类似于以下的大 df

A一种 B C C D D E
1 1 10 10 3 3 9 9 5 5
2 2 4 4 6 6 9 9 5 5
4 4 8 8 12 12 7 7 3 3

I am trying to make a scatter plot where column A is the x-values and all values in the rest of the columns are the y-values with each column a separate color.我正在尝试制作一个散点图,其中 A 列是 x 值,其余列中的所有值都是 y 值,每列都有不同的颜色。 I have tried the following but receive the error Error: unexpected ']' in p1我尝试了以下操作,但收到错误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()

I have also tried x=get(colnames(df.p[1])) and x=df.p[1] but I always get the same error我也试过x=get(colnames(df.p[1]))x=df.p[1]但我总是得到同样的错误

I tried the solution here but got the error我在这里尝试了解决方案但出现错误

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()

Created on 2021-07-15 by the reprex package (v2.0.0)reprex 包( v2.0.0 ) 于 2021 年 7 月 15 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM