简体   繁体   English

如何在x轴上使用分类变量在R中制作散点图?

[英]How to make a scatterplot in R with categorical variables on the x axis?

This question has been asked before but somehow the answers are not helpful for me.以前有人问过这个问题,但不知何故,答案对我没有帮助。

So I have the following data;所以我有以下数据; NW are different forests, which are differentiated in two groups ("Gruppe"), some have been fenced (Z) some have not (KZ). NW 是不同的森林,分为两组(“Gruppe”),有些已被围栏(Z)有些没有(KZ)。 Then for both groups of each forest, I have calculated the biodiversity (Shannon-Index, H) and the Evenness (E).然后对于每个森林的两组,我计算了生物多样性(香农指数,H)和均匀度(E)。

Here a short section of the data (i am not allowed to upload pictures yet :D)这是数据的一小部分(我还不允许上传图片:D)

I would like to create two scatterplots, one for the Shannonindex and one for the Eveness, where on the x-axis the forests (NW) are displayed and the different groups of each forest are compared/next to each other.我想创建两个散点图,一个用于香农指数,一个用于 Eveness,其中在 x 轴上显示森林 (NW),并且每个森林的不同组相互比较/相邻。 I tried to do so and the problem is, that R uses the NW as a scale and not each data point is labeled.我试图这样做,但问题是,R 使用 NW 作为尺度,而不是每个数据点都被标记。

I want it to look roughly like this , just not as a barplot, instead, each datapoint should simply be a point.我希望它看起来大致像这样,而不是作为条形图,相反,每个数据点应该只是一个点。

graph_data3 <- tapply(Shannon$H, list(Shannon$Gruppe, Shannon$NW),sum)

barplot((graph_data3),
        beside = T,
        las = 2,
        ylim = c(0,2),
        xlab = "Naturwald Nummer",
        ylab = "Shannonindex",
        main = "Durchschnittliche Biodiversität (Shannonindex)",
        legend = T,
        args.legend = list (x=90, y=2),
        col = c("paleturquoise3","forestgreen")
)

It seems to be such a dumb question but I just can't fix it.这似乎是一个愚蠢的问题,但我无法解决它。 I hope it is understandable what my problem is.我希望我的问题是可以理解的。

So I created a short dataset to demonstrate what I think you want: let me know if you need me to make modifications.所以我创建了一个简短的数据集来展示我认为你想要的:如果你需要我进行修改,请告诉我。

I used tidyverse for this, which has packages like dplyr and ggplot .我为此使用了tidyverse ,它有dplyrggplot等包。

df <- data.frame(ID = rep(c("A", "B", "C"), each = 2), type = rep(c("head", "shoulder")), avg = abs(rnorm(6)))

The data df looks like:数据df看起来像:

  ID     type        avg
1  A     head 1.05664244
2  A shoulder 0.39070804
3  B     head 1.07129057
4  B shoulder 0.73273048
5  C     head 0.01978039
6  C shoulder 0.60652992

Using this data:使用这些数据:

df %>% 
ggplot(aes(x = type, y = avg, group = type, fill = type, color = type)) + 
geom_point() + 
facet_wrap( ~ ID)

Which looks like看起来像ggplot 结果与方面

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

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