我想在融化的数据帧中绘制变量的分散(xy)图,如下所示。
df
class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00 我试过了,但它画了4分。如何绘制x和y?
library(ggplot2)
ggplot(df, aes(x=mean, y=mean, group=var, colour=class)) +
geom_point( size=5, shape=21, fill="white")发布于 2015-10-19 12:05:57
正如Heroka所指出的,您需要数据采用更宽的类型格式。如果数据是这样读取的,您可以使用以下方法来转换它。
## you don't need this since you already have df
text = "class var mean
0 x 4.25
0 y 6.25
1 x 2.00
1 y 11.00"
df = read.delim(textConnection(text),header=TRUE,strip.white=TRUE,
stringsAsFactors = FALSE, sep = " ");df2
## use this library to switch from long-wide
library(reshape2)
df2 = dcast(df, class ~ var, value.var = "mean")
library(ggplot2)
ggplot(df2, aes(x=x, y=y, colour=class)) +
geom_point( size=5, shape=21, fill="white")

https://stackoverflow.com/questions/33213475
复制相似问题