我很难将图例文本对齐到左侧.
library(ggplot2)
library(reshape2)
o3<- rnorm(1827, 50, 10)
NO2<- rnorm(1827, 35, 7)
NOx<- rnorm(1827, 45, 10)
pm25<- rnorm(1827, 15, 4)
date<-seq(as.Date('2000-01-01'),as.Date('2004-12-31'),by = 1)
df<-data.frame(date,o3,NO2,NOx,pm25)
meltdf <- melt(df,id="date")
Run Code Online (Sandbox Code Playgroud)
使用此代码,对齐自动向左
ggplot(meltdf, aes(x = date, y = value, colour =variable)) + geom_smooth() + stat_smooth(method = "gam")
Run Code Online (Sandbox Code Playgroud)
然而,以下是alignemt到中心.
ggplot(meltdf, aes(x = date, y = value, colour =variable)) +
geom_smooth() + stat_smooth(method = "gam") +
scale_color_discrete(name="Pollutant" ,labels = c(expression(O[3]),
expression(NO[2]),
expression(NO[x]),
expression(PM[2.5])))
Run Code Online (Sandbox Code Playgroud)
我怎么能用最后一个脚本实现左对齐?
尝试grouped_df
通过索引选择类对象的列给出"错误:索引超出范围".例如
x <- mtcars %>% group_by(am, gear) %>% summarise_each(funs(sum), disp, hp, drat)
class(x)
# "grouped_df" "tbl_df" "tbl" "data.frame"
# For some reason the first column can be selected...
x[1]
# Source: local data frame [4 x 1]
# Groups: am
# am
# 0
# 0
# 1
# 1
# ...but any index > 1 fails
x[2]
# Error: index out of bounds
# Coercing to data frame does the trick...
as.data.frame(x)[2]
# gear
# 3
# …
Run Code Online (Sandbox Code Playgroud) 假设我有这样的数据框
d <- data.frame(id = 1:5, var1 = rep(1, 5), var2 = rep(2, 5),
var3 = rep(3, 5), m = seq(0, 1, length.out = 5))
d
# id var1 var2 var3 m
# 1 1 2 3 0.00
# 2 1 2 3 0.25
# 3 1 2 3 0.50
# 4 1 2 3 0.75
# 5 1 2 3 1.00
Run Code Online (Sandbox Code Playgroud)
我想要实现的是结果
d[c("var1", "var2", "var3")] <- d[c("var1", "var2", "var3")] * d$m
Run Code Online (Sandbox Code Playgroud)
但mutate()
在一次通话中使用(即没有写d %>% mutate(var1 = var1 …