小编shi*_*iny的帖子

在ggplot2中创建特定的日期范围(scale_x_date)

嗨,我有从2010年到2050年的年度数据.我正在尝试制作线图,以便我的x轴从2010年开始到2050年结束,显示5年间隔iei希望x轴断裂为(2010年,2015年,2020年......) .,2045,2050).不幸的是,它从2013年开始到2048年结束.为什么?怎么解决这个问题?我的数据,代码和结果图片如下.

plot1<- ggplot(test, aes(yr, y=value)) +
geom_line()+
scale_x_date(breaks = date_breaks("5 year"),labels=date_format("%Y")) +
scale_y_continuous(breaks=seq(-4,6, by=1))
plot1
Run Code Online (Sandbox Code Playgroud)

数据

 dput(test)
structure(list(value = c(2.47099989218436, 3.09640452148661, 
1.32121989082519, 0.742309399667898, 0.180070229458727, 2.2475619117108, 
0.606470664265897, 2.12742331755353, 2.73894680327422, 2.22368873465667, 
1.58381022102847, 2.10136510397371, 1.74582199030396, 2.21689521610787, 
2.51618709804907, 1.87243814589322, 1.92029935267449, 1.79383249007138, 
0.537680017904451, 1.2415782984683, 3.62075008273724, 4.50975793125965, 
3.70660640492563, 4.16317150909305, 2.24008439109747, 2.24587596633027, 
3.63019754286973, 4.28513572439197, 3.61013179034863, 4.20010027834161, 
2.06766292535187, 4.34833637648799, 5.71460894423653, 4.12185659615561, 
3.93305702163007, 3.29384139246081, 3.2915580598453, 4.21009646693621, 
4.32889796119913, 4.99213117815761), yr = structure(c(14610, 
14975, 15340, 15706, 16071, 16436, 16801, 17167, 17532, 17897, 
18262, 18628, 18993, 19358, 19723, 20089, 20454, 20819, …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 timeserieschart

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

nearPoints无法从闪亮的coordinfo中自动推断出`xvar`

我试图ggplot使用该nearPoints功能在鼠标点击附近找到点,但它无法正常工作.我使用下面的代码创建了带有两个diamondsdata.frame 图的闪亮app :

library(shiny)
library(ggplot2)
ui <- fluidPage(
mainPanel(
uiOutput("tb")
)
)
server <- function(input,output){

   output$diamonds1 <- renderPlot({

        print(ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                                geom_point(alpha=0.5)+ facet_wrap(~color, scales="free"))
   })
   output$diamonds2 <- renderPlot({

        print(ggplot(diamonds, aes(x=carat, y=price, col=clarity)) + 
                               geom_point(alpha=0.5)+ facet_wrap(~cut, scales="free"))
   }) 

   output$info <- renderPrint({

        nearPoints(diamonds, input$plot_click, threshold = 10, maxpoints = 1,
                   addDist = TRUE)
   })

output$tb <- renderUI({
tabsetPanel(tabPanel("First plot", 
                     plotOutput("diamonds1")),
            tabPanel("Second plot", 
                     plotOutput("diamonds2", click = "plot_click"), 
                     verbatimTextOutput("info"))) 
})
}
shinyApp(ui = ui, server = …
Run Code Online (Sandbox Code Playgroud)

plot r ggplot2 shiny

3
推荐指数
1
解决办法
1457
查看次数

从Dropbox读取.csv文件,并将其绘制在闪亮的应用程序的传单地图中

我要在地图上绘制4个测站。您可以从此处下载数据。

使用下面的代码,我设法绘制了它们

library(shiny)
library(leaflet)
stations <- read.csv("~path/stations.csv")
ui <- bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%")) 
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet(stations) %>%
  addProviderTiles("Esri.WorldTopoMap") %>%
  addCircleMarkers(~x,~y)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

这是结果

在此处输入图片说明

现在,在最终shiny应用程序中,我想.csv从保管箱读取文件。

按照此链接中描述方法,我尝试了以下方法

library(repmis)
myfilename <- "stations.csv"
mykey <- "i9pw95ltjown2uc"
stations <- source_DropboxData(myfilename, key=mykey, sep=",", header=TRUE)
Run Code Online (Sandbox Code Playgroud)

我得到这个错误

source_DropboxData(myfilename,key = mykey,sep =“,”,header = TRUE)中的错误:未使用的参数(myfilename,key = mykey,sep =“,”,header = TRUE)

使用 …

csv r dropbox leaflet shiny

3
推荐指数
1
解决办法
962
查看次数

如何使用闪亮来绘制上传的数据集?

我是R-shiny应用程序的新手,我的应用程序非常简单.它有两个选项卡,在第一个,我上传一个文件,如csv,然后在第二个选项卡中,我选择将要绘制的列,我的解决方案分散在几个例子,每个都与我的不一样,我想要上传的数据集有待观察,并且可以在所有功能中使用,而不仅仅是在上传时.

我的服务器.R

library(shiny)
shinyServer(function(input, output) {
output$contents <- renderTable({
inFile <- input$file1

if (is.null(inFile))
  return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep, 
         quote=input$quote)

})
output$MyPlot <- renderPlot({
x    <- contents()$contents[, c(input$xcol, input$ycol)]
bins <- nrow(contents())
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Run Code Online (Sandbox Code Playgroud)

ui.R

library(shiny)
library(datasets)
shinyUI(fluidPage(
titlePanel("Column Plot"),
tabsetPanel(
tabPanel("Upload File",
         titlePanel("Uploading Files"),
         sidebarLayout(
           sidebarPanel(
             fileInput('file1', 'Choose CSV File',
                       accept=c('text/csv', 
                                'text/comma-separated-values,text/plain', 
                                '.csv'))

      ),
           mainPanel(
             tableOutput('contents')
           )
         )
),
tabPanel("First Type",
         pageWithSidebar(
           headerPanel('My First Plot'),
           sidebarPanel(
             selectInput('xcol', 'X Variable', names(content)), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 rstudio shiny

3
推荐指数
1
解决办法
5628
查看次数

dplyr:同一公式中全年的每日价值和特定每日价值之和

使用dfdata.frame

date <- rep(as.Date(seq(as.Date("2003-01-01"), 
                        as.Date("2005-12-31"), by = 1), 
                    format="%Y-%m-%d"), 9)
site <- c(rep("Site_1", 3*1096), rep("Site_2", 3*1096), rep("Site_3", 3*1096))
rain <- c(rep(as.numeric(sample(1.1e6:87e6, 1096, replace=T)),3),
               rep(as.numeric(sample(1.3e5:56e6, 1096, replace=T)),3),
               rep(as.numeric(sample(5e5:77e6, 1096, replace=T)),3))
parameter <- rep(c(rep("param_A", 1096), rep("param_B", 1096), rep("param_c", 1096)), 3)
value <- c(runif(1096, 0.005, 2.3)/1e6, 
           runif(1096, 0.5, 3.1)/1e6,
           runif(1096, 0.003, 0.04)/1e6,
           runif(1096, 0.002, 1.7)/1e6, 
           runif(1096, 0.3, 4.5)/1e6,
           runif(1096, 0.001, 0.07)/1e6,
           runif(1096, 0.007, 2.7)/1e6, 
           runif(1096, 0.4, 2.8)/1e6,
           runif(1096, 0.004, 0.09)/1e6)

df <- data.frame( date, site, rain, parameter, value)
df[c(1:4, 8:10, 30:35, …
Run Code Online (Sandbox Code Playgroud)

r dplyr

3
推荐指数
1
解决办法
143
查看次数

ggplot:如何将常见的x和y标签添加到绘图网格

使用diamonds,我想绘制caratVS price为(4级FairGoodVery GoodPremimum的)cut

相反,允许facet_wrap()控制在断轴,我做了四幅土地以控制轴的断裂。

library(ggplot2)
library(egg)
library(grid)
f1 <- 
ggplot(diamonds[diamonds$cut=="Fair",], aes(carat, price))+
  geom_point()+
  facet_wrap(~cut, ncol =2)+
  scale_x_continuous(limits = c(0,4), breaks=c(0,  1, 2, 3, 4))+
  scale_y_continuous(limits = c(0,10000), breaks=c(0,  2500, 5000, 7500, 10000))+
  labs(x=expression(" "),
       y=expression(" "))


f2 <- 
ggplot(diamonds[diamonds$cut=="Good",], aes(carat, price))+
  geom_point()+
  facet_wrap(~cut, ncol =2)+
  scale_y_continuous(limits = c(0,5000), breaks=c(0,  1000, 2000, 3000, 4000, 5000))+
  labs(x=expression(" "),
       y=expression(" "))


f3 <- 
  ggplot(diamonds[diamonds$cut=="Very Good",], aes(carat, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

3
推荐指数
1
解决办法
4172
查看次数

facet_zoom 正在放大到不同的 x 轴值

使用这个

数据

df <- read.table(text = c("
Site    measured    simulated
site1   9.1 6.8
                          site2 163.1   128.1
                          site3 126 75.9
                          site4 741.2   843
                          site5 2215.1  1937.7
                          site6 283.6   423.4
                          site7 115.6   92.5
                          site8 12.1    15.3
                          site9 13.4    15.8
                          site10    475.7   296.1
                          site11    1   1.4
                          site12    84.5    131.9
                          site13    74.1    43.9
                          site14    19.2    33.3
                          site15    74.8    41.1
                          site16    287.8   366.9"), header =T)
Run Code Online (Sandbox Code Playgroud)

和下面的脚本

library(tidyr) 
library(dplyr)
library(ggplot2)
library(ggforce)
df_reorder <-  transform(df, 
                         Site_reorder = reorder(Site, -measured))
df_long <- df_reorder %>% gather("id", "value", 2:3) 


ggplot(df_long, …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggforce

3
推荐指数
1
解决办法
1845
查看次数

在R中填写起始值和结束值之间的值

我的 data.frame 中的 W(下面的蓝线)表示河流中的水位与高程剖面相交的位置。

在此输入图像描述

在我的 data.frame 中,对于 ID 中的每个组,我需要填写起始值和结束值 (W) 之间的值

我的数据

> head(df, 23)
   ID elevation code
1   1       150 <NA>
2   1       140 <NA>
3   1       130    W
4   1       120 <NA>
5   1       110 <NA>
6   1       120 <NA>
7   1       130    W
8   1       140 <NA>
9   1       150 <NA>
10  2        90 <NA>
11  2        80 <NA>
12  2        70 <NA>
13  2        66    W
14  2        60 <NA>
15  2        50 <NA>
16  2 …
Run Code Online (Sandbox Code Playgroud)

r

3
推荐指数
1
解决办法
685
查看次数

如果开始月份不是1月,则分配年份ID

我有一个df data.frame由8年的每日价值观组成的.

date <- rep(as.Date(seq(as.Date("2001-05-01"),
                    as.Date("2008-04-30"), by= 1), format="%Y-%m-%d"), 3)

site <- c(rep("Site_1", 2557), rep("Site_2", 2557), rep("Site_3", 2557))

value <- c(as.numeric(sample(90:271, 2557, replace=T)),
           as.numeric(sample(125:340, 2557, replace=T)),
           as.numeric(sample(70:173, 2557, replace=T)))

df <- data.frame(date, site, value)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,每年从5月开始到4 结束.

我想要得到的mean,并sdvalue每个year在3 sites.

我做了以下

df1 <- df %>%
  dplyr::mutate(year = ifelse(date < "2002-05-01", "2001-2002",
                              ifelse(date < "2003-05-01", "2002-2003",
                                     ifelse(date < "2004-05-01", "2003-2004",
                                            ifelse(date < "2005-05-01", "2004-2005",
                                                   ifelse(date < "2006-05-01", "2005-2006", …
Run Code Online (Sandbox Code Playgroud)

r dplyr

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

Ifelse:将3个类别合并为一个

使用此data.frame:

df <- read.table(text = c("
ID  cat1    cat2    cat3
site1   High    High    High
site1   High    High    Medium
site1   High    High    Low
site1   High    Medium  High
site1   High    Medium  Medium
site1   High    Medium  Low
site1   High    Low High
site1   High    Low Medium
site1   High    Low Low
site1   Medium  High    High
site1   Medium  High    Medium
site1   Medium  High    Low
site1   Medium  Medium  High
site1   Medium  Medium  Medium
site1   Medium  Medium  Low
site1   Medium  Low High
site1   Medium  Low Medium
site1   Medium …
Run Code Online (Sandbox Code Playgroud)

if-statement r

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

标签 统计

r ×10

ggplot2 ×5

shiny ×3

dplyr ×2

plot ×2

csv ×1

dropbox ×1

ggforce ×1

if-statement ×1

leaflet ×1

rstudio ×1

timeserieschart ×1