小编ale*_*han的帖子

保存在闪亮的应用程序中制作的情节

我正在试图找出如何使用downloadButton来保存有光泽的情节.包中的示例演示了downloadButton/downloadHandler以保存.csv.我将基于此制作一个可重现的示例.

对于 ui.R

shinyUI(pageWithSidebar(
  headerPanel('Downloading Data'),
  sidebarPanel(
selectInput("dataset", "Choose a dataset:", 
            choices = c("rock", "pressure", "cars")),
    downloadButton('downloadData', 'Download Data'),
    downloadButton('downloadPlot', 'Download Plot')
  ),
  mainPanel(
    plotOutput('plot')
  )
))
Run Code Online (Sandbox Code Playgroud)

对于 server.R

library(ggplot2)
shinyServer(function(input, output) {
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  plotInput <- reactive({
    df <- datasetInput()
    p <-ggplot(df, aes_string(x=names(df)[1], y=names(df)[2])) +
      geom_point()
  })

  output$plot <- renderPlot({
    print(plotInput())
  })

  output$downloadData <- downloadHandler(
    filename = function() { paste(input$dataset, '.csv', sep='') },
    content = function(file) { …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 shiny

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

ggplot2和gridExtra:完全删除facet_grid中的strip - 不仅仅是不可见的

我有两个图表,我将它们放在另一个上面,方式如下:

library(ggplot2)
library(gridExtra)
p1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p2 <- p2 + facet_grid(cyl ~ .)
grid.arrange(p1, p2, ncol=1)
Run Code Online (Sandbox Code Playgroud)

为此我需要顶部和底部图形的x轴排成一行,但是由于左边的条带,刻面图形比顶部图形窄.我可以使用以下方法使条带不可见:

theme(strip.text.y = element_blank())
theme(strip.background = element_blank())
Run Code Online (Sandbox Code Playgroud)

然而,这并没有摆脱条带所占据的空间.所以我要么需要一种方法来完全摆脱条带,或者有办法将我的分面图分割成单独的图形,但不知何故在它们之间共享相同的y轴标签.在我的图表中,我有两个不是很高的刻面板,并且没有足够的空间让每个都有一个像样的大小的y轴.

有什么建议?

r ggplot2 gridextra

16
推荐指数
1
解决办法
7394
查看次数

将值添加到闪亮的反应表中

我希望我的闪亮应用程序的用户能够迭代地向表中添加元素,但我无法弄清楚如何保存值.

在此示例中,我希望用户能够在文本框中添加值,这些值应添加到主面板中表格的底部.目前,之前添加的值已丢失.

library(shiny)

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                     sidebarPanel(textInput("text1", "Column 1"),
                                  textInput("text2", "Column 2"),
                                  actionButton("update", "Update Table")),
                     mainPanel(tableOutput("table1"))),
  server=function(input, output, session) {
    tableStart <- data.frame(Column1 = NA, Column2 = NA)
    newEntry <- reactive({
      input$update
      newLine <- isolate(c(input$text1, input$text2))
      })
    output$table1 <- renderTable({rbind(tableStart, newEntry())})
  }))
Run Code Online (Sandbox Code Playgroud)

r shiny

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

如何下标x轴刻度标签

我用下面的脚本生成了这个图. 在此输入图像描述 但我怎么能下标PM10中的"10",SO2中的"2"和NO2中的"2"?

我试过了levels(df$variable) <- c("PM[10]","SO[2]", "NO", "NO[2]"),但没办法.

有人可以帮忙吗?谢谢!

variable <- c("PM10","SO2","NO","NO2")
coef <- c(10,20,30,40)
coef_lb <- c(-5,10,23,27)
coef_ub <- c(20,39,39,50)

df <- as.data.frame(cbind(variable, as.numeric(coef),as.numeric(coef_lb),as.numeric(coef_ub)))


df$variable <- factor(df$variable,levels=c("PM10","SO2","NO","NO2"))
levels(df$variable) <- c("PM[10]","SO[2]", "NO", "NO[2]")

library(ggplot2)
#ggplot 95%CI
BWplot <- ggplot(data=df,aes(x=variable,y=coef))
BWplot <- BWplot + geom_pointrange(aes(ymin=coef_lb,ymax=coef_ub))
BWplot <- BWplot + geom_point() 
BWplot <- BWplot + scale_y_continuous(limits=c(-110, 110),breaks=seq(-100, 100, by = 20))
BWplot <- BWplot + xlab("Air pollutant") 
BWplot <- BWplot + ylab("Mean change") 
BWplot <- BWplot + geom_hline(yintercept=0,alpha=0.5)
BWplot
Run Code Online (Sandbox Code Playgroud)

r ggplot2 axis-labels

10
推荐指数
1
解决办法
3845
查看次数

在R中读取.tif文件

我正在读取R中的.tif文件并获得下面列出的4条警告消息.当我按照第4条消息的说明进行操作时,前3个警告仍然存在,但从文件读取的值在每个像素处都会发生剧烈变化.请帮助我从.tif文件中正确读取数据.示例文件可以在链接上找到:ftp://ftp.ntsg.umt.edu/pub/MODIS/NTSG_Products/MOD16/MOD16A2_MONTHLY.MERRA_GMAO_1kmALB/GEOTIFF_0.05degree/

我的代码:

remove(list=ls()) 

library(tiff)

library(raster)

str_name<-'MOD16A2_ET_0.05deg_GEO_2008M01.tif' 

read_file<-readTIFF(str_name) 
Run Code Online (Sandbox Code Playgroud)

警告信息:

1: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 33550 (0x830e) encountered
2: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 33922 (0x8482) encountered
3: In readTIFF(str_name) :
  TIFFReadDirectory: Unknown field with tag 34735 (0x87af) encountered
4: In readTIFF(str_name) :
  tiff package currently only supports unsigned integer or float sample formats in direct mode, but the image contains signed integer format - it will be treated as unsigned (use …
Run Code Online (Sandbox Code Playgroud)

r

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

在ggplot中围绕常见值添加矩形

当我进行实验设计时,我使用ggplot来显示布局.这是一个简单的例子:

df <- data.frame(Block=rep(1:2, each=18),
            Row=rep(1:9, 4),
            Col=rep(1:4, each=9),
            Treat=sample(c(1:6),replace=F))
Run Code Online (Sandbox Code Playgroud)

我将描绘如下:

df.p <- ggplot(df, aes(Row, Col)) + geom_tile(aes(fill=as.factor(Treat)))
Run Code Online (Sandbox Code Playgroud)

给:

设计

有时我在设计中有一个结构我想通过在它周围放一个框来强调,例如主图.在这种情况下:

df$Mainplot <- ceiling(df$Row/3) + 3*(ceiling(df$Col/2) - 1)
Run Code Online (Sandbox Code Playgroud)

然后我使用geom_rect和一些需要调整每个设计的混乱代码来生成类似的东西:

designwithmainplot

问题:如何以简单的方式在主图周围添加矩形?这似乎是一个简单的问题,但我还没有找到一个明显的方法.我可以将颜色或其他美学映射到主图,但我似乎无法用一个盒子包围它们.任何指针都非常感激.

r ggplot2

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

在lapply/ldply中使用列表中的对象名称

在试图回答一个问题之前,我遇到了一个似乎应该很简单的问题,但我无法弄明白.

如果我有一个数据帧列表:

df1 <- data.frame(a=1:3, x=rnorm(3))
df2 <- data.frame(a=1:3, x=rnorm(3))
df3 <- data.frame(a=1:3, x=rnorm(3))

df.list <- list(df1, df2, df3)
Run Code Online (Sandbox Code Playgroud)

我想要rbind在一起,我可以做到以下几点:

df.all <- ldply(df.list, rbind)
Run Code Online (Sandbox Code Playgroud)

但是,我想要另一个列来标识data.frame每行来自哪一行.我希望能够使用该deparse(substitute(x))方法(此处和其他地方)获取相关的名称data.frame并添加一列.这就是我接近它的方式:

fun <- function(x) {
  name <- deparse(substitute(x))
  x$id <- name
  return(x)
}
df.all <- ldply(df.list, fun)
Run Code Online (Sandbox Code Playgroud)

哪个回报

  a          x      id
1 1  1.1138062 X[[1L]]
2 2 -0.5742069 X[[1L]]
3 3  0.7546323 X[[1L]]
4 1  1.8358605 X[[2L]]
5 2  0.9107199 X[[2L]]
6 3  0.8313439 …
Run Code Online (Sandbox Code Playgroud)

r plyr

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

在select_中构造变量名

我正在尝试编写一个函数,它将(部分)通过组合其源数据框和现有变量名来重命名变量.从本质上讲,我想:

df1 <- data.frame(a = 1, b = 2)
Run Code Online (Sandbox Code Playgroud)

成为:

df1 %>%
  rename(df1_a = a)

#  df1_a b
#1     1 2
Run Code Online (Sandbox Code Playgroud)

但是我想以编程方式执行此操作,类似于:

fun <- function(df, var) {
  outdf <- rename_(df, paste(df, var, sep = "_") = var)
  return(outdf)
}
Run Code Online (Sandbox Code Playgroud)

这种天真的方法显然不起作用,但我无法弄明白.我确定答案就在nse插图中(https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html),但这似乎并不能解决构造变量名称的问题.

r dplyr

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

在R中查找长度大于1的向量元素

我有这样的矢量

c(0,1,2,0,0,2,2,2,2,2,2,1,0,1,2,2,2,2,2,1)
Run Code Online (Sandbox Code Playgroud)

我想找到一系列至少连续三个连续2,c(2,2,2)开始的位置,如果它被中断,我想找到下一个第一个位置.

返回向量应如下所示:

FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)

我尝试了匹配和其他几个功能但没有成功

r

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

在p值的ggplot geom_tile中格式化颜色和图例

我目前正在尝试使用ggplot2制作一个'热图'来显示一系列p值,但无法弄清楚如何定制实际的颜色分配和图例.

sampledata.m <- melt(sampledata)
sampledata.m$var2 <- as.character(sampledata.m$var2)
sampledata.m$var2 <- factor(sampledata.m$var2, levels=unique(sampledata.m$var2),ordered=TRUE)
sampledata.m$var1 <- as.character(sampledata.m$var1)
sampledata.m$var1 <- factor(sampledata.m$var1, levels=unique(sampledata.m$var1),ordered=TRUE)
Run Code Online (Sandbox Code Playgroud)

这样做是为了保持变量的顺序.

p <- ggplot(sampledata.m, aes(var2, var1)) + 
  geom_tile(aes(fill = value), colour = "transparent") +    
  scale_fill_gradientn(colours=c("light green","dark green", "black"),
    values=rescale(c(0,0.0003,0.05,0.5,1)),limits=c(0,1)))
p + theme_bw(base_size = base_size) + labs(x = "", y = "") + 
  scale_x_discrete(expand = c(0,0)) + 
  theme(legend.position = "bottom", axis.ticks = element_blank(), 
    axis.text.x = element_text(size = base_size * 0.8, angle = 310, 
    hjust = 0, colour = "black"))
Run Code Online (Sandbox Code Playgroud)

这会创建一个漂亮的情节,但我的图例和我的颜色渐变并不代表我指定的重新缩放.原谅我的无知,如果这是一个简单的修复,但我现在只编写了R大约2周.理想情况下,我会喜欢我的情节和传奇,以模仿与本文类似的配色方案和图例标签:http://www.ncbi.nlm.nih.gov/pubmed/22496159 …

r colors legend ggplot2 p-value

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

如何得到R中两组的差异?

假设:

x <- c(2,-5)
y <- c(1,2,3)
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到 :

z = c(2-1, 2-2, 2-3, -5-1, -5-2, -5-3) = c(1, 0, -1, -6, -7, -8)
Run Code Online (Sandbox Code Playgroud)

r

4
推荐指数
3
解决办法
128
查看次数

R lapply重置rownames的数据帧列表

您可以通过运行重置数据框中的rownames

>rownames(df) <- NULL
Run Code Online (Sandbox Code Playgroud)

我有一个数据帧列表,并希望重置列表中每个数据帧的所有rownames,我试过

>newlist <- llply(mylist, function(df) { rownames(df) <- NULL })
Run Code Online (Sandbox Code Playgroud)

Bu tit不起作用,返回NULLS列表,原件保持不变.

r dataframe

4
推荐指数
2
解决办法
1670
查看次数

在R中按模式重命名列

我想通过特定模式重命名数据框中的所有列.

我的意见:

Log.NE122  Log.NE244  Log.NE144
-0.33       0.98        1.0
Run Code Online (Sandbox Code Playgroud)

我的预期产量:

 NE122  NE244  NE144
-0.33   0.98    1.0
Run Code Online (Sandbox Code Playgroud)

干杯.

r rename renaming

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

标签 统计

r ×13

ggplot2 ×5

shiny ×2

axis-labels ×1

colors ×1

dataframe ×1

dplyr ×1

gridextra ×1

legend ×1

p-value ×1

plyr ×1

rename ×1

renaming ×1