简体   繁体   English

R中的组带状图

[英]stripchart by groups in R

I am still a newbie to R and would like to ask for help in graphing stripcharts.我仍然是 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)

This is my code at the moment and I am trying to group it by another column called "weightclass";这是我目前的代码,我正在尝试将其按名为“weightclass”的另一列进行分组; Basically using another color for each weightclass.基本上为每个 weightclass 使用另一种颜色。 "weightclass" has 4 values: 1, 2, 3, 4 respectively. “weightclass”有 4 个值:分别为 1、2、3、4。 Is there something I can do to easily do so?有什么我可以轻松做到的吗?

Thank you for the help!感谢您的帮助!

Here's an example using mtcars :这是使用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编辑-1

With ggplot there's a bit of a way to get what you asked for all in one strip and colored by group:使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM