[英]ggplot2: using geom_point() and not all of my character labels are plotting on the X-axis
I am trying to graph using the ggplot2
geom_point
call in R
.我正在尝试使用
R
的ggplot2
geom_point
调用绘制图形。 However, when I plot the desired graph, my X-labels that are words (not numbers) do not all show up on the X-axis.但是,当我绘制所需的图形时,作为单词(而非数字)的 X 标签并未全部显示在 X 轴上。
To begin, here is some reproducible data:首先,这里有一些可重复的数据:
Bac <- data.frame(logFC = seq(-1, 3.5, 0.19),
ASV_Fam = c("ASV_31; Bdellovibrionaceae", "ASV_152; Reyranellaceae", "ASV_102; Hymenobacteraceae", "ASV_124; Nitrospiraceae", "ASV_141; NA",
"ASV_180; Microscillaceae", "ASV_259; Microscillaceae", "ASV_272; Chitinophagaceae", "ASV_79; Chthoniobacteraceae",
"ASV_266; Chthoniobacteraceae", "ASV_106; Nitrosomonadaceae", "ASV_121; Nitrospiraceae", "ASV_184; Methylophilaceae", "ASV_115; Chthoniobacteraceae",
"ASV_123; Nitrosomonadaceae", "ASV_143; Haliangiaceae", "ASV_139; NA", "ASV_159; Micrococcaceae", "ASV_185; Xanthobacteraceae", "ASV_227; Chitinophagaceae",
"ASV_233; NA", "ASV_239; Chitinophagaceae", "ASV_255; NA", "ASV_204; Longimicrobiaceae"),
Phylum = c("Bdellovibrionota", "Proteobacteria", "Bacteroidota", "Nitrospirota",
"Proteobacteria", "Bacteroidota", "Bacteroidota", "Bacteroidota",
"Verrucomicrobiota", "Verrucomicrobiota", "Proteobacteria", "Nitrospirota",
"Proteobacteria", "Verrucomicrobiota", "Proteobacteria", "Myxococcota",
"Proteobacteria", "Actinobacteriota", "Proteobacteria", "Bacteroidota",
"Proteobacteria", "Bacteroidota", "Cyanobacteria","Gemmatimonadota"))
Bac$Family <- gsub("^[^.]*;", "", Bac$ASV_Fam)
The closest I have found to my error is this post: Unable to plot points from a data.frame .我发现最接近我的错误的是这篇文章: Unable to plot points from a data.frame 。 Following instructions from there, I added a factor with a single level by following the suggested code:
按照那里的说明,我按照建议的代码添加了一个具有单个级别的因子:
Bac$logFC <- factor(Bac$logFC, levels = unique(Bac$logFC))
Bac$ASV_Fam <- factor(Bac$ASV_Fam, levels = unique(Bac$ASV_Fam))
Graphing:绘图:
ggplot(Bac, aes(x = Family, y = logFC, color = Phylum)) + geom_point() +
scale_x_discrete(labels = toShow$ASV_Fam) + theme(axis.text.x = element_text(colour = "black", size = 9, angle = -90))
However, this still does not plot all of my X-labels that I need to see.但是,这仍然没有绘制我需要查看的所有 X 标签。 Here is the graph I get:
这是我得到的图表:
As you can see, it only plotted 14 of my 24 labels I have passed for the X-axis.如您所见,它只绘制了我为 X 轴传递的 24 个标签中的 14 个。 All of my points are there, but some vertical lines show more than 1 point and only 1 label is associated with that vertical line.
我所有的点都在那里,但一些垂直线显示超过 1 个点,并且只有 1 个标签与该垂直线相关联。 See for example X-axis labels:
ASV_152; Reyranellaceae
例如,参见 X 轴标签:
ASV_152; Reyranellaceae
ASV_152; Reyranellaceae
, ASV_102; Hymenobacteraceae
ASV_152; Reyranellaceae
, ASV_102; Hymenobacteraceae
ASV_102; Hymenobacteraceae
, ASV_266; Chthoniobacteraceae
ASV_102; Hymenobacteraceae
, ASV_266; Chthoniobacteraceae
ASV_266; Chthoniobacteraceae
, etc. ASV_266; Chthoniobacteraceae
等。
I am not sure why these are not given separate X-axis labels and are instead being graphed on the same vertical line, thus reducing the total labels plotted on the X-axis.我不确定为什么这些没有被赋予单独的 X 轴标签,而是在同一条垂直线上绘制,从而减少了在 X 轴上绘制的总标签。
Other work arounds I have tried: widening the pdf via the pdf()
command, widening the graph by passing coord_fixed(ratio = 0.25)
, but none of these options work.我尝试过的其他解决方法:通过
pdf()
命令加宽pdf()
,通过传递coord_fixed(ratio = 0.25)
加宽图形,但这些选项都不起作用。
In addition, passing the following code scale_y_discrete(breaks = seq(-1, 4, 0.5))
so that I can not have so many numbers show up in the Y-axis does not work.此外,传递以下代码
scale_y_discrete(breaks = seq(-1, 4, 0.5))
以便我不能在 Y 轴上显示这么多数字是行不通的。 I think it's because the y-axis has been set as a factor, so I tried to keep it numeric, but that isn't working either.我认为这是因为 y 轴已被设置为一个因子,所以我尝试将其保留为数字,但这也不起作用。
Any clue into what is going on would be very helpful!任何有关正在发生的事情的线索都会非常有帮助!
For reference, here is the ouput of my session sessionInfo()
作为参考,这是我的会话
sessionInfo()
的输出
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6
When you check dim(table(Bac$Family))
, there exists 14
unique values in Family
, and as you set x axis of your plot by x = Family
, there only 14 values exists.当您检查
dim(table(Bac$Family))
存在14
中唯一值Family
,并为您设置X坐标轴的情节x = Family
,有14只存在价值。
I'm not sure about your purpose, but if you want all 24 labels( ASV_Fam
) in x axis and one point per each, instead of x = Family
, try x = ASV_Fam
我不确定你的目的,但如果你想要 x 轴上的所有 24 个标签(
ASV_Fam
),每个标签一个点,而不是x = Family
,试试x = ASV_Fam
Please let me know if this plot is not what you wanted.如果这个情节不是你想要的,请告诉我。
ggplot(Bac, aes(x = ASV_Fam, y = logFC, color = Phylum)) + geom_point() +
theme(axis.text.x = element_text(colour = "black", size = 9, angle = -90))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.