小编Seb*_*Seb的帖子

在ggplot2中使用边缘直方图的散点图

有没有办法用边缘直方图创建散点图,就像下面的示例一样ggplot2?在Matlab中它是scatterhist()函数,并且R也存在等价物.但是,我还没有看到ggplot2.

具有边缘直方图的散点图

我开始尝试创建单个图形,但不知道如何正确排列它们.

 require(ggplot2)
 x<-rnorm(300)
 y<-rt(300,df=2)
 xy<-data.frame(x,y)
     xhist <- qplot(x, geom="histogram") + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 5/16, axis.text.y = theme_blank(), axis.title.y=theme_blank(), background.colour="white")
     yhist <- qplot(y, geom="histogram") + coord_flip() + opts(background.fill = "white", background.color ="black")

     yhist <- yhist + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 16/5, axis.text.y = theme_blank(), axis.title.y=theme_blank() )


     scatter <- qplot(x,y, data=xy)  + scale_x_continuous(limits=c(min(x),max(x))) + scale_y_continuous(limits=c(min(y),max(y)))
none <- qplot(x,y, data=xy) + geom_blank()
Run Code Online (Sandbox Code Playgroud)

并使用 …

r histogram scatter-plot ggplot2

130
推荐指数
12
解决办法
5万
查看次数

gc()和rm()有什么区别

我定期用R调用来清理R中的内存rm(list=ls()).
之后我需要打电话给垃圾收集器gc()吗?

这两个功能有什么区别?是否gc()要求rm()某些变量?

garbage-collection memory-management r

59
推荐指数
1
解决办法
4万
查看次数

从Rdata文件中获取特定对象

我有一个Rdata包含各种对象的文件:

 New.Rdata
  |_ Object 1  (e.g. data.frame)
  |_ Object 2  (e.g. matrix)
  |_...
  |_ Object n
Run Code Online (Sandbox Code Playgroud)

当然我可以加载数据框load('New.Rdata'),但是,是否有一种智能方法只从该文件中加载一个特定对象并丢弃其他对象?

r object rdata

50
推荐指数
4
解决办法
1万
查看次数

ggplot2:更改轴上因子变量的显示顺序

当我使用geom_tile()ggplot2和离散缩放标签在x轴按升序排列,在y轴降序排列:

#some sample data
a <- runif(400)
a <- matrix(a, ncol=20)
colnames(a) <- letters[seq( from = 1, to = 20 )]
rownames(a) <- letters[seq( from = 1, to = 20 )]
a <- melt(a)
Run Code Online (Sandbox Code Playgroud)

当我绘制数据帧时,a这就出现了:

ggplot(a, aes(X1, X2, fill = value)) + geom_tile() + 
scale_fill_gradient(low = "white",  high = "black", breaks=seq(from=0, to=1, by=.1), name="value") + 
opts(axis.text.x=theme_text(angle=-90, hjust=0)) +
scale_x_discrete(name="") + scale_y_discrete(name="") 
Run Code Online (Sandbox Code Playgroud)

并且对于x和y标记的coords不同:

在此输入图像描述

我想让标签从上到下,从左到右排序.有一个快速的方法来做到这一点?

r ggplot2

21
推荐指数
2
解决办法
3万
查看次数

用透明色ggplot2创建箱图

我创建了一个包含多个组的图形,并在一行线上绘制了一个geom_boxplot().但是,透明地对盒子进行着色以便可以看到线条会很不错.

这是一些示例数据:

x11()

name <- c("a", "a", "a", "a", "a", "a","a", "a", "a", "b", "b", "b","b", "b", "b","b", "b", "b")
class <- c("c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3","c1", "c1", "c1", "c2", "c2", "c2", "c3", "c3", "c3")
year <- c("2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008", "2010", "2009", "2008")
value <- c(100, 33, 80, 90, 80, 100, 100, 90, 80, 90, 80, 100, 100, 90, 80, 99, 80, 100)

df …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 boxplot

18
推荐指数
2
解决办法
2万
查看次数

子集参数如何在lm()函数中起作用?

我一直试图弄清楚subsetR lm()函数中的参数是如何工作的.特别是下面的代码对我来说似乎很可疑:

 data(mtcars)
 summary(lm(mpg ~ wt,  data=mtcars))
 summary(lm(mpg ~ wt, cyl, data=mtcars))
Run Code Online (Sandbox Code Playgroud)

在每种情况下,回归都有32个观察结果

  dim(lm(mpg ~ wt, cyl  ,data=mtcars)$model)
  [1] 32  2
   dim(lm(mpg ~ wt  ,data=mtcars)$model)
  [1] 32  2
Run Code Online (Sandbox Code Playgroud)

然而系数改变(连同R²).该帮助没有提供太多关于此事的信息:

子集一个可选向量,指定要在拟合过程中使用的观察子集

r linear-regression lm

7
推荐指数
1
解决办法
2万
查看次数

如何获得stan中参数的完全边际分布

stan 网页开始标准示例时,如下所示:

schools_code <- '
  data {
   int<lower=0> J; // number of schools 
   real y[J]; // estimated treatment effects
   real<lower=0> sigma[J]; // s.e. of effect estimates 
 } 
 parameters {
   real theta[J]; 
   real mu; 
   real<lower=0> tau; 
 } 
 model {
   theta ~ normal(mu, tau); 
   y ~ normal(theta, sigma);
 } 
 '

  schools_dat <- list(J = 8, 
                 y = c(28,  8, -3,  7, -1,  1, 18, 12),
                 sigma = c(15, 10, 16, 11,  9, 11, 10, 18))

 fit <- stan(model_code = schools_code, …
Run Code Online (Sandbox Code Playgroud)

r winbugs14 stan

6
推荐指数
2
解决办法
1548
查看次数

替换r中不同编码的特殊字符

我有一个损坏的文件,其中Windows特殊字符已被其UTF-8"等效"替换.我试着写一个能够替换基于这个表的特殊字符的函数:

utf2win <- function(x){
soll <- c("À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", 
  "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", 
  "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", 
  "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", 
  "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ"
)

ist <- c("À", "Ã", "Â", "Ã", "Ä", …
Run Code Online (Sandbox Code Playgroud)

r utf-8 character-encoding windows-1252

6
推荐指数
1
解决办法
1952
查看次数

如何在闪亮的服务器上加快宣传单

我已经在闪亮的地方设置了一个简单的传单地图,其中闪亮的server.R样子如下:

(请从Dropbox获取RDS数据以获得可重现的示例)

Server.R

test_polygons <- readRDS('test_polygons.RDS') # Sind die Polygon-Shapefiles transformiert auf WGS84 für Bezirke

#some merging....
#we use sample data instead

test_polygons@data$sample <- runif(nrow(test_polygons@data))

#Create some nice popups
world_popup <- function(modell){
  niveau <- test_polygons@data[, modell]

  probs  <- seq(0, 1, length.out = 5 +  1)
  niveau <- cut(niveau, breaks=quantile(niveau, probs, na.rm = TRUE, names = FALSE), labels=paste('level', 0:4), include.lowest = TRUE)
  niveau <- as.character(niveau)


  niveau <- factor(niveau, labels=)

  paste0("<strong>Bezirk: </strong>", 
         as.character(test_polygons@data$ID), 
         "<br><strong><br>", 
         "</strong>", 
         "<strong>Level: </strong>", 
         niveau …
Run Code Online (Sandbox Code Playgroud)

r leaflet shiny

6
推荐指数
1
解决办法
1718
查看次数

ggplot2中的日期刻度显示错误的标签

我正在显示时间序列数据,ggplot2但是刻度标签显示了一些奇怪的行为.可能我做错了但我在互联网上找不到任何帮助.这是一个例子:

#just sample data
time <- as.Date(seq(as.Date("2004/1/1"), as.Date("2009/12/1"), by = "1 month"))
data <- rnorm(nrow(test))+c(1:nrow(test))
test <- data.frame(time, data)
Run Code Online (Sandbox Code Playgroud)

我用:

q1 <- ggplot(data=test) + geom_line(aes(x=time, y=data))
q1 <- q1 + scale_x_date(major="years", minor="3 months", format="%Y-%m", lim=c(as.Date("2004/1/1"),as.Date("2009/12/1")), name="") 
q1
Run Code Online (Sandbox Code Playgroud)

这会产生以下图表: 示例图

但根据我的理解,电网应该在2009/12/1结束 - 对吧?非常感谢你的帮助!

r ggplot2

5
推荐指数
1
解决办法
2248
查看次数