r - 如何将图例添加到 ggplot
问题描述
我已经查看了在 ggplot 上绘制图例的答案。但是,这些方法似乎都不适用于我的代码,如下所示:
library(readxl)
library(ggplot2)
library(dplyr)
inputManhattan <- read_xlsx("PlotsManhattan.xlsx")
PercentMN <- inputManhattan$Percent
ComNormMN <- inputManhattan$ComNorm
ElevationMN <- inputManhattan$Elevation
ggplot(inputManhattan, aes(PercentMN, ComNormMN)) +
geom_point(shape = 22, size = ElevationMN/20, col='orange', alpha = 0.5, fill = "grey50") +
geom_smooth(method = "loess", se = FALSE, color = 'orange') +
xlab("Slope") + ylab("Total Normalized Complaints")
我得到了这张图,其中斜率是针对投诉绘制的,海拔由正方形的大小表示: Manhattan Plot
我希望我的图例是:“高程 = 正方形的大小”,或者如果我可以在我的图例中使用插图,那就太好了。
但我什至无法让传奇出现。我会很感激任何帮助谢谢。
一些数据值: 数据
解决方案
你有没有尝试过:
ggplot(inputManhattan, aes(Percent, ComNorm)) +
geom_smooth(method = "loess", se = FALSE, color = 'orange') +
geom_point(
aes(size = Elevation/20),
shape = 22,
col = 'orange',
alpha = 0.5,
fill = "grey50") +
xlab("Slope") +
ylab("Total Normalized Complaints")
inputManhattan
数据集中的任何列名都放在aes()
.
在我上面的示例代码中,您不需要PercentMN
,ComNormMN
和ElevationMN
.
我使用数据集运行了类似的代码mtcars
并得到了一个图例:
ggplot(mtcars, aes(disp, mpg)) +
geom_smooth(method = "loess", se = FALSE, color = 'orange') +
geom_point(
aes(size = wt/20),
shape = 22,
col = 'orange',
alpha = 0.5,
fill = "grey50")
推荐阅读
- elasticsearch - Elasticsearch 中范围查询的奇怪行为
- azure - ADF 限制:一个管道中的 COPY 数据活动数量?
- botframework - Bot Framework 技能 - 无需身份验证即可调试
- postgresql - 如何在 PostgreSQL 中使用单个查询将两条相关记录插入两个不同的表中?
- .net - .NET Core BackgroundService 只运行一次
- matlab - 如何在 MATLAB 中将 3D 坐标 (x,y,z) 转换为 .STL 文件?
- python - SyntaxError: f-string: 期待 '}'
- html - Visualforce:仅当值不为空时才呈现抛出异常
- reactjs - 如何在反应引导模态中的模态内容的div上添加类
- javascript - 尝试通过 multer 的存储保存图像时,为什么我得到“ENOENT:没有这样的文件或目录,打开”?