use*_*704 5 r ggplot2 shiny-server
我正在创建一个闪亮的应用程序,在他选择了他感兴趣的日期范围(= x轴的范围)后向用户显示ggplot.所以我想我需要定义一个被动数据对象(正确吗?).
ggplot中有一些子集.R告诉我反应数据对象不是子集.在我对ggplot的新手理解中,必须在geom_bar(),geom_line()语句中完成子集化,以获得我想要的图形.
如何在为图形生成颜色时引用因子类别?谢谢!
A = c(3, 4, 3, 5)
B = c(2, 2, 1, 4)
Z = c(1, 2, 1, 2)
R = c(-2, -1, -3, 0)
S = c(7,7,7,9)
mydata = data.frame(cbind(A,B,Z,R,S))
dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
mydata$date = as.Date(dates)
mydata.m = melt(mydata,id="date")
names(mydata.m) = c("variable", "category","value")
Run Code Online (Sandbox Code Playgroud)
data.r = reactive({
a = subset(mydata.m, variable %in% input$daterange)
return(a)
})
Run Code Online (Sandbox Code Playgroud)
output$myplot = renderPlot({
# ggplot with proper reference to reactive function <<data.r()>>
s = ggplot(data.r(), aes(x=variable, fill=category)) +
# bars for categories A, B, Z: needs subsetting the data... but how?
+ geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack")
+ geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity")
# lines for categories R, S: same.
+ geom_line(subset=.(category=="R"), aes(y=value))
+ geom_line(subset=.(category=="S"), aes(y=value))
# how to reference the factor <<category>> in reactive function <<data.r()>>?
+ scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))
print(s)
})
Run Code Online (Sandbox Code Playgroud)# INPUT PART
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("My App"),
sidebarPanel(
dateRangeInput("daterange", "Date range:",
start = "2014-01-01",
end = "2014-04-01",
min = "2014-01-01",
max = "2014-04-01",
format = "dd/mm/yyyy",
separator = "-"),
submitButton(text="Update!")
),
# -----------------------------------------------
# OUTPUT PART
mainPanel(
tabsetPanel(
tabPanel("Tab 1", h4("Head 1"),plotOutput("myplot"))
)
)
))
Run Code Online (Sandbox Code Playgroud)
library(reshape)
library(shiny)
library(ggplot2)
# GEN DATA -----------------------------------------------
A = c(3, 4, 3, 5)
B = c(2, 2, 1, 4)
Z = c(1, 2, 1, 2)
R = c(-2, -1, -3, 0)
S = c(7,7,7,9)
mydata = data.frame(cbind(A,B,Z,R,S))
dates = c("2014-01-01","2014-02-01","2014-03-01","2014-04-01")
mydata$date = as.Date(dates)
mydata.m = melt(mydata,id="date")
names(mydata.m) = c("variable", "category","value")
# SERVER -----------------------------------------------
shinyServer(function (input, output) {
# DATA
data.r = reactive({
a = subset(mydata.m, variable %in% input$daterange)
return(a)
})
# GGPLOT
mycolorgenerator = colorRampPalette(c('sienna','light grey'))
output$myplot = renderPlot({
# ggplot with proper reference to reactive function <<data.r()>>
s = ggplot(data.r(), aes(x=variable, fill=category)) +
# bars for categories A, B, Z: needs subsetting the data... but how?
geom_bar(data=subset(data.r(), category %in% c("A","B")), aes(y=value), stat="identity", position="stack") +
geom_bar(subset=.(category=="Z"), aes(y=-value), stat="identity") +
# lines for categories R, S: same.
geom_line(subset=.(category=="R"), aes(y=value)) +
geom_line(subset=.(category=="S"), aes(y=value)) +
# how to reference the factor <<category>> in reactive function <<data.r()>>?
scale_fill_manual(breaks = levels(category), values = mycolorgenerator(length(levels(category))))
print(s)
})
})
Run Code Online (Sandbox Code Playgroud)
(完整的server.R和ui.R真的有帮助)
我不确定你.()从哪里获得功能或者geom_bar有subset=参数的想法.但这里有一个更新renderPlot,似乎至少没有产生任何错误
output$myplot = renderPlot({
dd<-data.r()
# ggplot with proper reference to reactive function <<data.r()>>
s = ggplot(dd, aes(x=variable, fill=category)) +
# bars for categories A, B, Z: needs subsetting the data... but how?
geom_bar(data=subset(dd, category %in% c("A","B")), aes(y=value),
stat="identity", position="stack") +
geom_bar(data=subset(dd, category=="Z"), aes(y=-value), stat="identity") +
# lines for categories R, S: same.
geom_line(data=subset(dd, category=="R"), aes(y=value)) +
geom_line(data=subset(dd, category=="S"), aes(y=value)) +
scale_fill_manual(breaks = levels(dd$category),
values = mycolorgenerator(length(levels(dd$category))))
print(s)
})
Run Code Online (Sandbox Code Playgroud)
大多数情况下,我更改data=为显式subset()调用
| 归档时间: |
|
| 查看次数: |
11555 次 |
| 最近记录: |