我想使用循环和 ggplot2 创建直方图网格。假设我有以下代码:
library(gridExtra)
library(ggplot2)
df<-matrix(NA,2000,5)
df[,1]<-rnorm(2000,1,1)
df[,2]<-rnorm(2000,2,1)
df[,3]<-rnorm(2000,3,1)
df[,4]<-rnorm(2000,4,1)
df[,5]<-rnorm(2000,5,1)
df<-data.frame(df)
out<-NULL
for (i in 1:5){
out[[i]]<-ggplot(df, aes(x=df[,i])) + geom_histogram(binwidth=.5)
}
grid.arrange(out[[1]],out[[2]],out[[3]],out[[4]],out[[5]], ncol=2)
Run Code Online (Sandbox Code Playgroud)
请注意,所有图都出现了,但它们都具有相同的平均值和形状,尽管将 df 的每一列设置为具有不同的平均值。
它似乎只绘制了最后一个图(out[[5]]),也就是说,循环似乎正在用 out[[5]] 重新分配所有 out[[i]]。
我不确定为什么,有人可以帮忙吗?
我创建了一个闪亮的应用程序,用于显示数据框中点的传单地图。
我想允许用户单击地图上的任意位置以获取附近点的一些信息并在该点上留下标记。
他们可能想点击其他地方。当他们单击其他地方时,我希望留下一个新标记,并删除旧标记。
我已经编写了一个工作闪亮的应用程序,但我无法让它工作。
我尝试使用clearMarkers,但这会删除所有标记,包括我创建的标记和底层数据帧中的标记。
我尝试指定单击点的 id,以便clearMarkers 可能只是删除该点,但我不知道由谁来找出单击点的 id。
我怎样才能让它发挥作用?
这是我的玩具代码:
library(shiny)
library(sp)
library(shinydashboard)
library(leaflet)
#### Make a spatial data frame
lats<-c(37.38,39)
lons<-c(-94,-95,-96)
df<-data.frame(cbind(lons,lats))
coordinates(df)<-~lons+lats
#### Define UI for application that draws a histogram
ui <- dashboardPage(
dashboardHeader(
),
# Sidebar layout with input and output definitions
dashboardSidebar(
),
# Main panel for displaying outputs
dashboardBody(
h2("My Map", align="center"),
h5("Click anywhere to draw a circle", align="center"),
leafletOutput("mymap", width="100%", height="500px")
),
)
#### Define server logic required to draw a histogram …Run Code Online (Sandbox Code Playgroud) 假设我有一个带有数据表和绘图的 Shiny 应用程序。我希望能够搜索/过滤数据表,并有一个反映结果的图。
我该怎么做呢?这甚至可能吗?有没有办法将过滤后的数据表输出到我可以使用的对象?
这是一个基本的闪亮应用程序,它不起作用。
library(DT)
ui <- basicPage(
h2("The mtcars data"),
DT::dataTableOutput("mytable"),
plotOutput('plot1')
)
server <- function(input, output) {
output$mytable = DT::renderDataTable({
datatable(mtcars,filter = 'top')
})
output$plot1 <- renderPlot({
plot(input$mytable$wt, input$mytable$mpg)
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)