R-从数据框创建散点图 [英] R - Creating Scatter Plot from Data Frame

查看:143
本文介绍了R-从数据框创建散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框all:

i've got a data frame all that look like this:

http://pastebin.com/Xc1HEYyH

现在,我想创建一个散点图,以x轴上的列标题和各自的值作为数据点.例如:

Now I want to create a scatter plot with the column headings in the x-axis and the respective values as the data points. For example:

7|                 x  
6|          x      x  
5|  x       x      x     x    
4|  x       x            x 
3|                             x      x  
2|                             x      x
1|
 ---------------------------------------
    STM    STM    STM   PIC   PIC    PIC
   cold   normal  hot  cold  normal  hot

这应该很容易,但是我不知道怎么做.

This should be easy, but I can not figure out how.

致谢

推荐答案

如果要使用Hadley的ggplot2进行绘制,则基本思想是获取以下形式的数据:

The basic idea, if you want to plot using Hadley's ggplot2 is to get your data of the form:

        x          y
col_names     values

这可以通过使用Hadley的reshape2中的melt函数来完成.执行?melt以查看可能的参数.但是,在这里,由于我们要融合整个data.frame,因此我们只需要

And this can be done by using melt function from Hadley's reshape2. Do ?melt to see the possible arguments. However, here since we want to melt the whole data.frame, we just need,

melt(all) 
# this gives the data in format:
#   variable value
# 1 STM_cold   6.0
# 2 STM_cold   6.0
# 3 STM_cold   5.9
# 4 STM_cold   6.1
# 5 STM_cold   5.5
# 6 STM_cold   5.6

在这里,x将是列variable,而y将是对应的value列.

Here, x will be then column variable and y will be corresponding value column.

require(ggplot2)
require(reshape2)
ggplot(data = melt(all), aes(x=variable, y=value)) + 
             geom_point(aes(colour=variable))

如果您不想要颜色,则只需删除geom_point内的aes(colour=variable),使其变为geom_point().

If you don't want the colours, then just remove aes(colour=variable) inside geom_point so that it becomes geom_point().

在这里我可能应该提到,您也可以将geom_point替换为geom_jitter,这会给您带来抖动点:

I should probably mention here, that you could also replace geom_point with geom_jitter that'll give you, well, jittered points:

这篇关于R-从数据框创建散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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