R 更改ggplot2散点图中的颜色
我正试图修改一些现有代码,这些代码最初来自此处()的问题,并用于在以下网站上演示R中的散点图: 这是一个简单而愚蠢的问题,但我整个上午都在苦苦思索。下面的代码给出了一个漂亮的黑白散点图。我想修改代码,使线条变成浅灰色R 更改ggplot2散点图中的颜色,r,ggplot2,R,Ggplot2,我正试图修改一些现有代码,这些代码最初来自此处()的问题,并用于在以下网站上演示R中的散点图: 这是一个简单而愚蠢的问题,但我整个上午都在苦苦思索。下面的代码给出了一个漂亮的黑白散点图。我想修改代码,使线条变成浅灰色 library(MASS) library(nlme) library(plyr) library(ggplot2) ### set number of individuals n <- 200 ### average intercept and slope beta0
library(MASS)
library(nlme)
library(plyr)
library(ggplot2)
### set number of individuals
n <- 200
### average intercept and slope
beta0 <- 1.0
beta1 <- 6.0
### true autocorrelation
ar.val <- .4
### true error SD, intercept SD, slope SD, and intercept-slope cor
sigma <- 1.5
tau0 <- 2.5
tau1 <- 2.0
tau01 <- 0.3
### maximum number of possible observations
m <- 10
### simulate number of observations for each individual
p <- round(runif(n,4,m))
### simulate observation moments (assume everybody has 1st obs)
obs <- unlist(sapply(p, function(x) c(1, sort(sample(2:m, x-1,
replace=FALSE)))))
### set up data frame
dat <- data.frame(id=rep(1:n, times=p), obs=obs)
### simulate (correlated) random effects for intercepts and slopes
mu <- c(0,0)
S <- matrix(c(1, tau01, tau01, 1), nrow=2)
tau <- c(tau0, tau1)
S <- diag(tau) %*% S %*% diag(tau)
U <- mvrnorm(n, mu=mu, Sigma=S)
### simulate AR(1) errors and then the actual outcomes
dat$eij <- unlist(sapply(p, function(x) arima.sim(model=list(ar=ar.val),
n=x) * sqrt(1-ar.val^2) * sigma))
dat$yij <- (beta0 + rep(U[,1], times=p)) + (beta1 + rep(U[,2], times=p)) *
log(dat$obs) + dat$eij
dat = ddply(dat, .(id), function(x){
x$alpha = ifelse(runif(n = 1) > 0.9, 1, 0.1)
x$grouper = factor(rbinom(n=1, size =3 ,prob=0.5), levels=0:3)
x
})
tspag = ggplot(dat, aes(x=obs, y=yij)) +
geom_line() + guides(colour=FALSE) + xlab("Observation Time Point") +
ylab("Y")
spag = tspag + aes(colour = factor(id))
spag
bwspag = tspag + aes(group=factor(id))
bwspag
库(MASS)
图书馆(nlme)
图书馆(plyr)
图书馆(GG2)
###设定个人数量
n是否要以灰度显示线条?如果是,那么在geom_line()
函数中添加color
就足够了。例如:
ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_line(colour = "gray40")
可以选择灰色的其他值:从0到100 完美,正是我所需要的。非常感谢。