x轴上具有类的列名称的散点图 [英] Scatter plot with column names on x-axis with classes

查看:38
本文介绍了x轴上具有类的列名称的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个散点图,其中x轴是离散的(一周中的一天),而y轴是连续的(值),并且一个类指示器为每个数据点赋予颜色.

I want to make a scatter plot where the x-axis is discrete (day of the week) and the y-axis is continuous (values) and a class indicator which gives a color to each data point.

与此类似:在R中将列名称绘制为x轴

但是我不想使用数据中的所有列,而颜色是基于我不想要的观测值.

but I don't want to use all of the columns in the data and the colors were based on the observation number which I don't want.

示例数据:

DAT = data.frame(
  person = c(1:5),
  Mon = c(100, 98, 95, 99, 93),
  Tues = c(95, 88, 90, 91, 87),
  Wed = c(85, 80, 86, 81, 80),
  Thurs = c(84, 80, 77, 75, 74),
  Fri = c(66, 50, 20, 0, 72),
  Score = c("Y","Y","N","N","N")
)

ggplot(DAT, aes(x = c(Mon, Tues, Wed, Thurs, Fri), y = values, color = Score)) + geom_point()

我的try/pesuedo ggplot代码不起作用,但希望能给出我要完成的目标的想法.

my attempt/pesuedo ggplot code which does not work but hopefully gives an idea of what I'm trying to accomplish.

我试图将Mon-Fri设为数字(1,2,3,4,5),以便将其作为我的x坐标,将值作为y坐标,但这意味着行数是5倍.

I tried to make Mon-Fri as numbers (1,2,3,4,5) so that would be my x coordinates and the values would be the y-coordinates but that would mean 5 times the number of rows..

有什么想法吗?

推荐答案

您可以融化数据,然后绘制:

You could melt your data and then plot:

library(ggplot2)
library(reshape2)

DAT = data.frame(
  person = c(1:5),
  Mon = c(100, 98, 95, 99, 93),
  Tues = c(95, 88, 90, 91, 87),
  Wed = c(85, 80, 86, 81, 80),
  Thurs = c(84, 80, 77, 75, 74),
  Fri = c(66, 50, 20, 0, 72),
  Score = c("Y","Y","N","N","N")
)

DAT.melt = melt(DAT,id=c("person","Score"))

ggplot(DAT.melt, aes(x = variable, y = value, color = Score)) + geom_point()

这篇关于x轴上具有类的列名称的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆