Re: [问题] ggplot2 有关scale_fill_或Legends

楼主: celestialgod (天)   2017-06-07 20:26:47
※ 引述《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))
作者: BUQ (固执又暴燥)   2017-06-07 20:35:00
多谢哥 我会好好研究请问一下大大 `[`(j = fill := 这是什么意思
作者: clansoda (小笨)   2017-06-07 22:09:00
可以找一下datatable的介绍, 多个assign的function不对 我看错了

Links booklink

Contact Us: admin [ a t ] ucptt.com