简体   繁体   English

R图-分层的数值和分类多个变量

[英]R Plot - Multiple Variables, Numeric and Categorical, Layered

Still learning R, and have been struggling with plotting. 仍在学习R,并且一直在努力进行绘图。 Below is part of my data, and I will try to explain the type of plot: 以下是我的数据的一部分,我将尝试解释绘图的类型:

> head(bees.net.counts)
  Month Block Treatment Flower Bee_Richness Bee_Abundance
1   May     1        UB   POSI            1             1
2   May     2        DS   ERST            4            38
3   May     2        UB   RUBU            2             2
4   May     3        DS   ERST            3             4
5   May     3        DS   TROH            1            10
6   May     3        GS   ERST            1             1

I want to make a plot where Flower is on the x-axis (there are 54 different ones), Bee_Richness or Bee_Abundance is on the y-axis, different colored symbols for Block (n=4) and amount of shading in each of those symbols for Treatment (n=3) (ie Block 1 Treatment UB is a red circle unfilled, Block 1 Treatment DS is a circle with half shaded red, and Block 1 Treatment GS is fully shaded red). 我想绘制一个图,其中Flower在x轴上(有54种不同),Bee_Richness或Bee_Abundance在y轴上,块的不同颜色符号(n = 4)以及每个阴影中的阴影量治疗符号(n = 3)(即,方框1治疗UB是未填充的红色圆圈,方框1治疗DS是带有半阴影红色的圆圈,方框1治疗GS是完全阴影的红色)。

The problem I have is that each line is plotted instead of putting every point above a specific flower spp (there are multiple rows that have, say, CHFA, but those represent different Blocks and Treatments). 我的问题是,绘制每条线而不是将每个点都放在特定的花spp上方(有多行具有CHFA,但这些行代表不同的块和处理)。

I have also tried this by month, where I separated the four months to make different graphs (to limit the length of the x-axis). 我也按月尝试过,我将四个月分开制作了不同的图形(以限制x轴的长度)。 There are 10 records in May, with 4 different flower species. 五月有10个记录,有4种不同的花。 I still can't figure out a way to do this. 我仍然不知道这样做的方法。

Thank you for your help!! 谢谢您的帮助!!

Edit: Here is what I hope to get = plot idea 编辑:这是我希望得到的= 情节的想法

This uses the idea of @db 's solution, but improves the axis labels. 这使用@db解决方案的思想,但改进了轴标签。

plot(x = as.numeric(as.factor(df$Flower)), df$Bee_Richness, 
    pch = as.numeric(as.factor(df$Block)), 
    col = as.numeric(as.factor(df$Treatment)), 
    xaxt="n", xlab="Flower", ylab="Richness")
axis(1, at=1:length(levels(df$Flower)), 
    labels=levels(df$Flower))

花图

Some added explanation 一些补充说明

As you requested, the character is based on the Block. 根据您的要求,角色基于方块。 The color is based on the Treatment. 颜色基于处理。 Let's look at the color/Treatment. 让我们看一下颜色/处理。 The trick is that when you make Treatment a factor, each value is internally represented as an integer, so you can use as.numeric on the factor and it translates DS to 1, GS to 2 and UB to 3. That makes the argument 诀窍在于,当您将“治疗”作为一个因子时,每个值在内部都表示为整数,因此您可以在该因子上使用as.numeric,并将DS转换为1,将GS转换为2,将UB转换为3。
col = as.numeric(as.factor(df$Treatment)) give DS color 1 and so on. col = as.numeric(as.factor(df$Treatment))赋予DS颜色1,依此类推。 R uses the numbers 1-8 as some easy-to-access colors. R使用数字1-8作为一些易于访问的颜色。 Since you only need 3, this works fine. 由于您只需要3个,因此可以正常工作。 Similarly, 同样的,
pch = as.numeric(as.factor(df$Block)) picks characters 1 through 3 for the three Block values in the small test data. pch = as.numeric(as.factor(df$Block))为小型测试数据中的三个Block值选择字符1到3。

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

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