p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value)) p1 + scale_fill_continuous(guide = guide_legend()) 我们可以为它设置各种样式,如 # 设置图例标题的位置 p2 <- p1 + guides(fill = guide_legend(title = "LEFT", title.position = "left")) # 使用 element_text 设置图例...
#方法1 ggplot(chic, aes(x = date, y = temp, color = season)) + geom_point() + l...
legend.position参数指定图中的图例位置。可选值可以是"none"、"left"、"right"、"bottom"、"top"或二元素数值向量。plot.title参数在以下示例中也用于修改绘图的标题。最后,使用grid.arrange函数同时绘制两个图。 library(ggplot2)library(gridExtra)library(babynames)library(dplyr)dat<-babynames%>%filter(name%in...
theme(legend.text=element_text(face ="bold"), #图例的PM2.5、NO2字体改成bold字体 legend.position = "top", #图例位置改为图上方 legend.title=element_blank(), #隐藏图例“pollutant" strip.text = element_text(face ="bold",size = 10), #设置单个图的类别标题文本的字体及大小(即小图中的“北...
gg <- df_albert %>% ggplot(aes(x = fct_relevel(sex, c("Male", "Female")), y = ratio, fill = align)) # geom_bar() 绘制条形图, stat = "identity",意味着条形的高度表示数据数据的值 gg <- gg + geom_bar(stat = "identity", width = 0.8, position = position_stack()) ...
base <- ggplot(toy, aes(up, up)) + geom_point(aes(colour = txt), size = 3) + xlab(NULL) + ylab(NULL) #legend.position控制图例上下左右位置 p1 <- base + theme(legend.position = "left") p2 <- base + theme(legend.position = "right") # 默认图例在左边 ...
p <- ggplot(mtcars,aes(mpg,hp,colour=factor(cyl)))+geom_point() p## 基础绘图 002、删除图例标题 p + theme(legend.title = element_blank())## 删除图例标题 003、删除图例 p + theme(legend.position ="none") 004、设置图例位置 p + theme(legend.position ="top") ...
dat<-data.frame(x=1:5,y=1:5,p=1:5,q=factor(1:5),r=factor(1:5))p<-ggplot(dat,aes(x,y,colour=p,size=q,shape=r))+geom_point()p1<-p+guides(colour="colorbar",size="legend",shape="legend")p2<-p+guides(colour=guide_colorbar(),size=guide_legend(),shape=guide_legend())p3...
frame(x=1:20,y=1:20,color=letters[1:20]) p <- ggplot(df,aes(x,y))+geom_point(aes(color=color)) p+guide_legend(title = 'legend',nrow=4,ncol=5) #Error: Can't add `guide_legend(title = "legend", nrow = 4, ncol = 5)` to a ggplot object. #正确方法: p+guides(color=...
pg_plot+theme(legend.position = c(0.8,0.3)) 在ggplot2中,左下角的坐标是c(0,0),右上角的坐标是c(1,1),你可以自己设置想要放置的位置。需要注意的是,你设置的这个坐标是图例中心点的坐标,可以通过legend.justification设置图例的哪个位置放在你的坐标上。