我想知道是否有可能在其中绘制一个变量的行子集 x-y轴而不必扩展/重塑数据框?
library(tidyverse)
id <- 1:6
size <- c(5, 2, 3, 4, 2, 8)
colour <- rep(c("red", "blue", "green"), 2)
df <- data.frame(id, size, colour)
x11()
ggplot(data = filter(df, colour %in% c("blue", "red")),
aes(x = size[colour == "blue"],
y = size[colour == "red"])) +
geom_point()
答案 0 :(得分:1)
是的,有可能。一种解决方案是直接向aes
提交向量(IMO这是对ggplot2
的滥用,但是现在我想不出任何其他解决方案了。)
# Subset data once so we wouldn't need to subset twice for nrow
id <- 1:6
size <- c(5, 2, 3, 4, 2, 8)
colour <- rep(c("red", "blue", "green"), 2)
df <- data.frame(id, size, colour)
pd <- subset(df, colour %in% c("blue", "red"))
# Use dummy empty data.frame
library(ggplot2)
ggplot(data.frame(),
# Submit x,y values as vectors that go every second entry
aes(pd$size[seq(2, nrow(pd), 2)], pd$size[seq(1, nrow(pd), 2)])) +
geom_point()