作者celestialgod (天)
看板R_Language
标题Re: [问题] ggplot2 有关scale_fill_或Legends
时间Wed Jun 7 20:26:47 2017
※ 引述《BUQ (固执又暴燥)》之铭言:
: [问题类型]:
:
: 程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
:
: [软体熟悉度]:新手(没写过程式,R 是我的第一次)
: [问题叙述]:
: 请简略描述你所要做的事情,或是这个程式的目的
: [程式范例]:
:
: 我想改 图的Legends的文字内容 用google找到这教学
: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
: 其中的一段 http://ideone.com/g0dEeN
: http://i.imgur.com/tWPaACT.jpg 我想改标示上的文字也就是legend.text
: http://i.imgur.com/jyAuJmW.jpg
: Q1 为何范例 有时用scale_fill_manual 有时用scale_fill_discrete差别在哪?
: Q2 我的程式码 http://ideone.com/JB8Vjw 我需要画的是 geom_density
: 有很多行被注解是因为在网路上找了很多解法 试都没有用
: 没注解的 就是我现阶段妥协的画法
: http://i.imgur.com/J3NWTRi.jpg
: [环境叙述]:
: R version 3.3.3 (2017-03-06)
: Platform: x86_64-w64-mingw32/x64 (64-bit)
: Running under: Windows >= 8 x64 (build 9200)
: [关键字]:
: Legends ggplot2
都已经用ggplot2,就不要再花费太多心力再手动设定的部分,尽量让它帮你完成
就像推文说的转成long table即可:
画出来的图:
http://imgur.com/a/DLG1I
library(data.table)
library(plyr)
library(pipeR)
library(ggplot2)
DT <- data.table(mean = rnorm(1000), wweather = rnorm(1000, 2),
prmean = rnorm(1000, 2.5)) %>>%
melt(measure.vars = 1:3) %>>%
`[`(j = fill := mapvalues(variable, c("mean","wweather", "prmean"),
c("平均", "观测", "75百分位")))
ggplot(DT, aes(value, fill = fill)) +
geom_density(alpha = 0.3) +
labs(x = "温度", title = "2012 高雄市", fill = "温度") +
theme(plot.title = element_text(hjust = 0.5))
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.233.49.200
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1496838410.A.DBA.html
1F:推 BUQ: 多谢哥 我会好好研究 06/07 20:35
2F:推 BUQ: 请问一下大大 `[`(j = fill := 这是什麽意思 06/07 22:01
先忽略那个吧(摊手,反正那里只是利用variable这个变数把fill mapping出来
3F:→ clansoda: 可以找一下datatable的介绍, 多个assign的function 06/07 22:09
※ 编辑: celestialgod (36.233.49.200), 06/07/2017 22:10:49
4F:→ clansoda: 不对 我看错了 06/07 22:10