图例太多时,会挤压正图,显得正图展示区域很小,这时有必要缩小图例。


#################
# 减小ggplot图例
################# library(ggplot2)
p <- ggplot(mtcars,
aes(drat, mpg, color = factor(gear), shape = factor(vs))) +
geom_point(size = 2) +
theme_classic() +
theme(legend.position = c(0.1, 0.7))
p # Overwrite given size (2) to 0.5 (super small)
p <- p + guides(shape = guide_legend(override.aes = list(size = 0.5)))
p p <- p + guides(color = guide_legend(override.aes = list(size = 0.5)))
p p <- p + theme(legend.title = element_text(size = 3),
legend.text = element_text(size = 3))
p addSmallLegend <- function(myPlot, pointSize = 0.5, textSize = 3, spaceLegend = 0.1) {
myPlot +
guides(shape = guide_legend(override.aes = list(size = pointSize)),
color = guide_legend(override.aes = list(size = pointSize))) +
theme(legend.title = element_text(size = textSize),
legend.text = element_text(size = textSize),
legend.key.size = unit(spaceLegend, "lines"))
} # Apply on original plot
addSmallLegend(p) ##################
# 折叠图例文本
################## a <- (1:10)
b <- c(1,1.5,2,4,5,5.3,7,9,9.5,9.8)
places = c("Birmingham","Chester-le-street","Cambridge", "Newcastle-upon-Tyne","Peterborough","Cambridge", "Newcastle-upon-Tyne","Peterborough","Liverpool","Stratford-upon-Avon")
df1 = data.frame(a,b,places) library(ggplot2)
p <- ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3)
p #指定图例列数
library(scales)
p + guides(colour = guide_legend(nrow = 2))
p ##或换行
df1$places<-sub("-", "- \n ", df1$places)
p = ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3)
p

最新文章

  1. 简单使用Git和Github来管理自己的代码和读书笔记
  2. sql server如何分组编号
  3. .Net程序员之Python基础教程学习----列表和元组 [First Day]
  4. C++内存未释放的情况
  5. bzoj1832: [AHOI2008]聚会
  6. java中的上转型对象
  7. 【Unity3D自我记录】解决NGUI通过问题触发事件点
  8. 在Ubuntu下编译Assimp库
  9. accept功能
  10. css变量的用法——(--cssName)
  11. [Swift]LeetCode978. 最长湍流子数组 | Longest Turbulent Subarray
  12. object标签和embed标签
  13. 图片利用 new Image()预加载原理 和懒加载的实现原理
  14. MySQL准入规范及容量评估
  15. Golang字符串格式化
  16. python 面向对象编程 之 单例模式
  17. Java数据结构和算法(一)线性结构
  18. 解决Qt creator无法输入中文
  19. 检查正则表达式的工具:Regex Match Tracer
  20. [BZOJ 4033] 树上染色

热门文章

  1. 264.丑数II
  2. Prometheus基于文件的服务发现
  3. 状压dp学习笔记(紫例题集)
  4. ESD
  5. Linux线程互斥学习笔记--详细分析
  6. linux 内核源代码情景分析——linux 内核源码中的汇编语言代码
  7. 『学了就忘』Linux基础 — 16、Linux系统与Windows系统的不同
  8. 20191310Lee_yellow缓冲区溢出实验
  9. Access的分页代码
  10. 自定义容器tomcat应用