我正在编写一个R代码,允许用户从数据中选择列并绘制每个列的直方图.因此,我使用'for'循环使用ggplot2库生成所需数量的绘图并将它们保存在单个列表中.但我面临的问题是,在'for'循环的每次迭代中,列表中的所有对象都存储相同的图.因此,最终输出由直方图网格组成,标记不同但描绘相同(最后)列.
据我所知,这个问题是很老,我发现答案就在重命名GGPLOT2图表for循环和https://stat.ethz.ch/pipermail/r-help/2008-February/154438.html是一个有用的出发点.
我使用R中可用的标准瑞士生育数据集来生成图.这是代码: -
data_ <- swiss
data_ <- na.omit(data_)
u <- c(2, 3, 4, 5, 6)
plotData <- data_[,u]
bw <- 5
plotType <- 'probability'
library(ggplot2)
library(gridExtra)
histogramList <- vector('list', length(u))
if(plotType=='probability')
{
for(i in 1:length(u))
{
indexDataFrame <- data.frame(plotData[,i])
probabilityHistogram <- ggplot(indexDataFrame, aes(x=indexDataFrame[,1]))
histogramList[[i]] <- probabilityHistogram + geom_histogram(aes(y=..density..), binwidth=bw, colour='black', fill='skyblue') + geom_density() + scale_x_continuous(names(plotData)[i]) + opts(legend.position='none')
}
} else
if(plotType=='frequency')
{
for(i in 1:length(u))
{
indexDataFrame <- data.frame(plotData[,i])
probabilityHistogram <- ggplot(indexDataFrame, aes(x=indexDataFrame[,1]))
histogramList[[i]] …Run Code Online (Sandbox Code Playgroud) 我知道网上已经有一些材料可以回答我的问题,但它们似乎都不适合我.我想那是因为我不太了解Shiny的反应式编程.
所以,我希望创建一个界面,让用户选择一个文件fileInput,只在点击上传按钮时上传.我尝试了各种论坛的一些解决方案,但都没有.以下是我最近的尝试:
#ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
fileInput("in_file", "Input file:",
accept=c("txt/csv", "text/comma-separated-values,text/plain", ".csv")),
checkboxInput(inputId="is_header", label="Does the input file have column names?", value=TRUE),
actionButton("upload_data", "Upload Data"),
),
mainPanel(
tabsetPanel(
tabPanel("Original Data", tableOutput("orig_data"))
)
)
))
#server.R
library(shiny)
shinyServer(function(input, output, session) {
ra_dec_data <- reactive({
if(input$upload_data==0)
return(NULL)
return(isolate({
head(read_data(input$in_file$datapath, input$in_file$is_header), 50)
}))
})
output$orig_data <- renderTable({
ra_dec_data()
})
})
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,文件一旦被选中就会上传,并且上传按钮没有响应.
我的猜测是我所做的事情没有意义,所以请接受我的道歉,因为这样做非常糟糕.任何帮助将非常感激.谢谢!
我试图在R中创建一个交互式直方图,其宽度可以通过移动滑块或在文本框中输入值来调整.除此之外,我还想为用户提供一个保存特定箱宽度图的选项.
为此,我发现'aplpack'库的'gslider'功能是一个很好的起点.我试图修改它以满足我的目的,并了解更多有关Tcl/Tk结构的信息.但我现在卡住了,无法继续,主要是因为我还没有完全理解如何在函数之间捕获和传输滑块值.
以下是我还没有真正理解的代码片段.这些来自'gslider'函数的源代码.
# What is the rationale behind using the 'assign' function here and at
# other instances in the code?
img <- tkrplot::tkrplot(gr.frame, newpl, vscale = 1, hscale = 1)
tkpack(img, side = "top")
assign("img", img, envir = slider.env)
# I understand the below lines when considered individually. But collectively,
# I am having a difficult time comprehending them. Most importantly, where
# exactly is the slider movement captured here?
sc <- tkscale(fr, from = sl.min, to = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ggplot2创建6个数据变量的对图,并根据它们所属的k均值聚类对点进行着色.我阅读了令人印象深刻的'GGally'软件包的文档,以及Adam Laiacano [http://adamlaiacano.tumblr.com/post/13501402316/colored-plotmatrix-in-ggplot2]的非正式修复程序.不幸的是,我找不到任何方法来获得所需的输出.
这是一个示例代码: -
#The Swiss fertility dataset has been used here
data_ <- read.csv("/home/tejaskale/Ubuntu\ One/IUCAA/Datasets/swiss.csv", header=TRUE)
data_ <- na.omit(data_)
u <- c(2, 3, 4, 5, 6, 7)
x <- data_[,u]
k <- 3
maxIterations <- 100
noOfStarts <- 100
filename <- 'swiss.csv'
library(ggplot2)
library(gridExtra)
library(GGally)
kmeansOutput <- kmeans(x, k, maxIterations, noOfStarts)
xNew <- cbind(x[,1:6], as.factor(kmeansOutput$cluster))
names(xNew)[7] <- 'cluster'
kmeansPlot <- ggpairs(xNew[,1:6], color=xNew$cluster)
OR
kmeansPlot <- plotmatrix(xNew[,1:6], mapping=aes(colour=xNew$cluster))
Run Code Online (Sandbox Code Playgroud)
两个图都是创建的,但没有根据簇进行着色.
希望我在论坛上没有错过这个问题的答案,如果确实如此,请道歉.任何帮助将受到高度赞赏.
谢谢!
我希望ggplot2使用通用函数为每个数据点生成错误栏,该函数使用函数提取相同的列名names.以下是演示代码:
plotfn <- function(data, xind, yind, yerr) {
yerrbar <- aes_string(ymin=names(data)[yind]-names(data)[yerr], ymin=names(data) [yind]+names(data)[yerr])
p <- ggplot(data, aes_string(x=names(data)[xind], y=names(data)[yind]) + geom_point() + geom_errorbar(yerrbar)
p
}
errdf <- data.frame('X'=rnorm(100, 2, 3), 'Y'=rnorm(100, 5, 6), 'eY'=rnorm(100))
plotfn(errdf, 1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
运行此命令会出现以下错误:
Error in names(data)[yind] - names(data)[yerr] :
non-numeric argument to binary operator
Run Code Online (Sandbox Code Playgroud)
有什么建议?谢谢.
我已经在Vim写了一些R脚本了一段时间了.从一小时前开始,我开始面临一个问题,即每次输入下划线(_)时,它都会自动转换为<-.
为此我改变了什么设置?谢谢!