ggplot2--Legend(图例操作)

Base Plot

xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5))
p=ggplot(data=xy)+geom_point(aes(x=x,y=y,color=type))
p
[转载]ggplot2--Legend(图例操作)

legend.background (rect)

# color does not work, use colour instead
p+opts(legend.background=theme_rect(colour="purple",fill="pink",size=3,linetype="dashed"))
[转载]ggplot2--Legend(图例操作)

legend.key (rect)

p+opts(legend.key=theme_rect(colour='purple',fill='pink',size=0.5,linetype='dashed'))
[转载]ggplot2--Legend(图例操作)

legend.key.size (unit)

# to use unit, you need to load package grid, i.e. library(grid)
p+opts(legend.key.size=unit(2,'cm'))
[转载]ggplot2--Legend(图例操作)

legend.key.width (unit)

p+opts(legend.key.width=unit(5,'cm'))
[转载]ggplot2--Legend(图例操作)

legend.text (text)

p+opts(legend.text = theme_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 3, face = 'bold'))
[转载]ggplot2--Legend(图例操作)

change legend labels

# breaks=c('A','B'), 'A' and 'B' are the default legend in xy
p+scale_colour_hue(name="my legend", breaks=c('A','B'), labels=c('AAA','BBB'))
[转载]ggplot2--Legend(图例操作)

legend.title (text)

p+opts(legend.title = theme_text(colour = 'red', angle = 45, size = 10, hjust = 3, vjust = 7, face = 'italic'))
[转载]ggplot2--Legend(图例操作)

legend.position (?)

# get rid of legend
p+opts(legend.position='none')
# move legend to the left
p+opts(legend.position='left')
define relative coordinates on plot c(x, y) between 0 and 1
p+opts(legend.position=c(0.5,0.5))
[转载]ggplot2--Legend(图例操作) [转载]ggplot2--Legend(图例操作) [转载]ggplot2--Legend(图例操作)

legend.justification (?)

Justification defines which side of the legend that the legend.position coordinates refer to
  p+ opts(legend.justification = 'right', legend.position=c(0.25,0.3))
can use left, right, centre or numeric value (0 ≤ x ≤ 1)
[转载]ggplot2--Legend(图例操作)


Reference
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
R语言中,ggplot2是一个常用的数据可视化包,用于创建高质量的图形。当我们在使用ggplot2绘制图形时,可以通过添加图例来解释Y轴的含义。 要在ggplot2添加Y轴图例,可以使用`scale_y_continuous()`函数或`scale_y_discrete()`函数,具体取决于Y轴的数据类型。 如果Y轴是连续型数据(如数值型),可以使用`scale_y_continuous()`函数。该函数可以设置Y轴的标签、标题和图例的位置等属性。例如,以下是一个示例代码: ```R library(ggplot2) # 创建一个示例数据集 data <- data.frame(x = 1:10, y = 1:10, group = c(rep("A", 5), rep("B", 5))) # 绘制散点图,并添加Y轴图例 ggplot(data, aes(x, y, color = group)) + geom_point() + scale_y_continuous(name = "Y轴标题", breaks = seq(1, 10, by = 2), labels = c("1", "3", "5", "7", "9"), guide = guide_legend(title = "图例标题")) ``` 如果Y轴是离散型数据(如因子型),可以使用`scale_y_discrete()`函数。该函数可以设置Y轴的标签、标题和图例的位置等属性。以下是一个示例代码: ```R library(ggplot2) # 创建一个示例数据集 data <- data.frame(x = 1:10, y = factor(1:10), group = c(rep("A", 5), rep("B", 5))) # 绘制柱状图,并添加Y轴图例 ggplot(data, aes(x, fill = group)) + geom_bar() + scale_y_discrete(name = "Y轴标题", labels = c("一", "二", "三", "四", "五", "六", "七", "八", "九", "十"), guide = guide_legend(title = "图例标题")) ``` 以上代码中,`scale_y_continuous()`和`scale_y_discrete()`函数分别用于设置连续型和离散型Y轴的图例。你可以根据自己的数据类型选择适合的函数来添加Y轴图例

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值