【问题标题】:stripchart by groups in RR中按组的条形图
【发布时间】:2016-09-20 23:58:50
【问题描述】:

我还是 R 的新手,想在绘制条形图方面寻求帮助。

stripchart(Study_intensive$AGE, method="stack",
at=c(0.05), pch=20, cex=2, xaxt="n", frame.plot=F, main= "Age Range in weight group(yr)")
axis(1, at=seq(0, 75, by=5) , labels= seq(0, 75, by=5),  
cex.axis=0.75)

这是我目前的代码,我正在尝试将它按另一个名为“weightclass”的列进行分组;基本上为每个权重等级使用另一种颜色。 "weightclass" 有 4 个值:分别为 1、2、3、4。我可以做些什么来轻松做到这一点吗?

感谢您的帮助!

【问题讨论】:

    标签: r plot stripchart


    【解决方案1】:

    这是一个使用mtcars的例子:

    library(RColorBrewer)
    testColor=brewer.pal(6, 'RdBu')
    stripchart(mtcars$mpg~mtcars$gear, col=testColor, method="stack", pch=20, cex=2, xaxt="n", frame.plot=F, main= "Age Range in weight group(yr)")
    axis(1, at=seq(0, 75, by=5) , labels= seq(0, 75, by=5), cex.axis=0.75)
    

    EDIT-1

    使用ggplot,有一种方法可以将您所要求的全部内容集中到一个条带中并按组着色:

    library(ggplot2)
    library(RColorBrewer)
    testColor=brewer.pal(6, 'RdBu')
    mtcars$color=testColor[mtcars$gear] #to get the colors your after
    mtcars$strip=1 #to get them into a single strip
    ggplot(mtcars, aes(x=strip, y=mpg, color=color)) +
      geom_jitter(position=position_jitter(0.2)) + xlim(0, 2)
    

    【讨论】:

    • 非常感谢!!有没有办法让不同的齿轮相互重叠?
    • @Monklife,尝试使用ggplot 进行编辑,如果可行,那么您应该一切就绪。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2022-08-02
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    相关资源
    最近更新 更多