r - 为什么我的条形图没有显示我的图例?
问题描述
在这张图片中,您可以看到我的条形图没有图例
我的代码是:
library(ggbeeswarm)
library(ggpubr)
library(ggplot2)
ggplot(data = ciber.1, aes(fill = cell_type, x = wcc_group, y = measurement)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(values = c("dodgerblue4", "dodgerblue2", "deepskyblue1", "deeppink4",
"deeppink1", "hotpink3", "lightpink3", "lightpink2",
"lightpink1", "lightpink", "darkorchid4", "darkorchid1",
"mediumpurple3", "thistle3", "thistle2", "thistle",
"navajowhite3", "navajowhite1", "springgreen3",
"springgreen", "chocolate4", "indianred1")) +
theme(legend.position = 'none') +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
labs(x = "WCC Group", y = "Leukocytes", title = "Division of leukocytes in WCC Groups") +
theme(strip.background = element_rect(fill="lightblue", size=2, color="black")) +
theme(strip.text = element_text(size = 15, face = "bold")) +
scale_y_continuous(expand = expansion(mult = c(0.05,0.15) ))
解决方案
您需要删除+ theme(legend.position = "none")
这应该显示图例:
library(ggbeeswarm)
library(ggpubr)
library(ggplot2)
ggplot(data = ciber.1, aes(fill = cell_type, x = wcc_group, y = measurement)) +
geom_bar(position = "fill", stat = "identity") +
scale_fill_manual(values = c("dodgerblue4", "dodgerblue2", "deepskyblue1",
"deeppink4", "deeppink1", "hotpink3",
"lightpink3", "lightpink2", "lightpink1",
"lightpink", "darkorchid4", "darkorchid1",
"mediumpurple3", "thistle3", "thistle2",
"thistle", "navajowhite3", "navajowhite1",
"springgreen3", "springgreen", "chocolate4",
"indianred1")) +
labs(x = "WCC Group", y = "Leukocytes", title = "Division of leukocytes in WCC Groups") +
theme(strip.background = element_rect(fill="lightblue", size=2, color="black"),
strip.text = element_text(size = 15, face = "bold"),
axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(expand = expansion(mult = c(0.05,0.15) ))
您还可以在一个theme()
函数中包含所有主题参数。这可能更容易跟踪您包含的参数。
推荐阅读
- android - 从缓存读取的 Firestore 应用程序不起作用
- arrays - 在嵌套函数中更新静态数组而不创建临时数组?
- sql - 如何比较来自两个不同行的列并显示两列之间的百分比差异
- selenium-webdriver - 黄瓜功能文件在Java类中没有拾取步骤定义,但运行Junit后控制台没有错误
- mongodb - MongoDB find() - 试图构造一个复杂的日期查询
- ios - Swift Snapchat fetchSnapUserInfo() 'UserEntity' 值不是已解析的身份
- java - DownloadTask 永远不会获取内容并使应用程序崩溃
- git - 更改提交的父级
- opengl - 如何使用 3d 查找表并将其像素值映射到图像?
- java - 仅设置 id 而不是先从 DB 获取对象时,一对多不会在没有引用的情况下更新