我试图在R中复制以下图片,特别是 ggplot2
我能够绘制红色的rss轮廓线,但我不知道如何绘制钻石(如左图所示)."预期输出"应该是一种绘制具有给定边长的钻石的方法.
编辑:这是一个简短的可重现的例子,在下面的图中随机添加钻石:
mlb<- read.table('https://umich.instructure.com/files/330381/download?download_frd=1', as.is=T, header=T)
str(mlb)
fit<-lm(Height~Weight+Age-1, data = as.data.frame(scale(mlb[,4:6])))
points = data.frame(x=c(0,fit$coefficients[1]),y=c(0,fit$coefficients[2]),z=c("(0,0)","OLS Coef"))
Y=scale(mlb$Height)
X = scale(mlb[,c(5,6)])
beta1=seq(-0.556, 1.556, length.out = 100)
beta2=seq(-0.661, 0.3386, length.out = 100)
df <- expand.grid(beta1 = beta1, beta2 = beta2)
b = as.matrix(df)
df$sse <- rep(t(Y)%*%Y,100*100) - 2*b%*%t(X)%*%Y + diag(b%*%t(X)%*%X%*%t(b))
base <- ggplot() +
stat_contour(data=df, aes(beta1, beta2, z = sse),breaks = round(quantile(df$sse, seq(0, 0.2, 0.03)), 0),
size = 0.5,color="darkorchid2",alpha=0.8) +
scale_x_continuous(limits = c(-0.4,1))+
scale_y_continuous(limits = c(-0.55,0.4))+
geom_point(data = points,aes(x,y))+
geom_text(data …Run Code Online (Sandbox Code Playgroud) 我想删除指定列之前的所有列.
示例数据:在mpg,这些是按顺序列出的以下列名:
names(mpg)
[1] "manufacturer", "model", "displ", "year", "cyl", "trans", "drv", "cty",
[9] "hwy", "fl", "class"
Run Code Online (Sandbox Code Playgroud)
假设我要删除列" cyl" 之前的所有列,我会这样做:
mpg[-(1:4)]
Run Code Online (Sandbox Code Playgroud)
但在我的实际数据中,有时候指定列(即cyl)之前的列数会发生变化.因此,不是4列(manufacturer, model, displ, year)总是在所需的列(cyl)之前,有时会有3或7等.
如何调整此代码以确保cyl包含"?" 之后的所有列?
代码块#1和#2是相同的,除了行号14.代码块#1使用print()呼叫,代码块#2使用该View()呼叫.代码块#1工作正常.代码块#2给出了错误"Error in FUN(X[[i]], ...) : object 'cal.date' not found".为什么?
library(tidyverse)
set.seed(1)
graph.data <- tibble(cal.date = as.Date(40100:40129, origin = "1899-12-30"),
random_num = rnorm(30, 8, 5))
child_function <- function(df, variable, hor.line = 6) {
variable <- enquo(variable)
df <- df %>% mutate(mutation = 2 * !!variable, horizontal.line = hor.line)
}
parent_function <- function(df, date, variable, hor.line = 6) {
date <- enquo(date)
variable <- enquo(variable)
df <- df %>% child_function(!!variable, hor.line) %>% print() # LINE …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在R中制作英国和爱尔兰的地图,以便根据纬度和经度输入数据点.我对这个项目很新,所以我不确定.
我写了代码:
map("worldHires","uk", xlim=c(-12,2), ylim=c(49,59), col="gray90", fill=TRUE)
Run Code Online (Sandbox Code Playgroud)
给我一张英国的地图,但每当我尝试将爱尔兰纳入其中时,都会出现错误.
有没有人知道我是如何解决这个问题的?