首页
学习
活动
专区
圈层
工具
发布

散点图
EN

Stack Overflow用户
提问于 2015-10-19 11:41:44
回答 1查看 1.8K关注 0票数 0

我想在融化的数据帧中绘制变量的分散(xy)图,如下所示。

代码语言:javascript
复制
df
class var  mean        
0      x   4.25 
0      y   6.25 
1      x   2.00 
1      y  11.00 

我试过了,但它画了4分。如何绘制x和y?

代码语言:javascript
复制
library(ggplot2)

ggplot(df, aes(x=mean, y=mean, group=var, colour=class)) +
geom_point( size=5, shape=21, fill="white")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-19 12:05:57

正如Heroka所指出的,您需要数据采用更宽的类型格式。如果数据是这样读取的,您可以使用以下方法来转换它。

代码语言:javascript
复制
## 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")

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33213475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档