Ggplot 在其闪亮应用程序内的标签中显示时没有数字限制
Ggplot is displayed without numeric limitations in its labels inside shiny app
我有下面这个闪亮的应用程序,我在其中创建了一个动态散点图。问题是,当我在闪亮的应用程序外部创建我的 ggplot
时,我以更大且最易读的形式获得它,但在 shiny
内部,我得到的结果非常小,两个轴上没有数字标签。为什么会这样?
app.R
library(shiny)
library(ggplot2)
n<-structure(list(ADM3_PCODE = c("ML100101", "ML100201", "ML070301",
"ML070105", "ML100301", "ML070107", "ML100401", "ML070104", "BF560105",
"ML060502", "ML060302", "ML070101", "ML070202", "ML070303", "ML070204",
"ML070205", "ML070106", "ML070305", "ML070103", "ML070102"),
Acled_Health = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), Acled_no_inst = c(4, 0, 0, 0, 0, 0, 0,
0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0), Acled_Total = c(4,
0, 1, 0, 0, 0, 0, 0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0),
I3 = c(1.09376534720521, 2.98445151003512, 1.38675338665065,
2.71565793377472, 3.18930523219588, 2.90551791702655, 1.53290303794127,
1.06882818216774, 1.0422158290422, -0.278650206004393, 1.74575777732344,
1.06030283919043, 1.96049964093946, 2.63957106704249, 2.58417173100033,
2.28469585057556, 0.531106478645378, 2.52253985112635, 2.72742421204698,
0.943157796157148), I4 = c(2.97790487569313, 4.39377989487155,
4.20062305434793, 3.5875729546847, 2.87065592255387, 1.93698960334718,
2.32611240844526, 3.79504053511001, 3.26867102154575, 3.54402045486982,
2.59943279715003, 2.24078712967162, 3.7837957824595, 0.629466739192882,
2.23259745421704, 3.22143437813918, 2.832868377928, 1.85546575024811,
2.44473249718205, 1.27700101283808), I5 = c(4.50640751725631,
4.59205796716096, 2.81987303382463, 5.6939014694895, 4.84552750414595,
3.48919571832088, 3.90947133936061, 5.62442105113242, 5.46450373445726,
3.81405499661111, 3.64136973672331, 1.48012372278708, 3.45348478980314,
2.79266252795185, 2.92273131958892, 3.12803195965532, 4.8713736085137,
4.78116333258574, 5.07934714109017, 4.90711103863146), I6 = c(4.71964925827176,
7.88685736651118, 4.48033628274155, 4.63463388102439, 5.41071131184507,
4.49985156270955, 4.81313128120664, 5.16036902503232, 4.07947495928142,
3.76143006251051, 5.18794193926347, 5.60747967555871, 5.50504418957362,
2.85716269808936, 7.16407719804625, 4.77737451764469, 5.7199718544193,
6.27105214031155, 3.48487832187209, 5.00715544299671), ADM0_FR = c("Mali",
"Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Burkina Faso",
"Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali",
"Mali", "Mali", "Mali"), ADM3_FR = c("Ménaka", "Andéramboukane",
"Anchawadi", "Talataye", "Inékar", "Tin Hama", "Tidermène",
"Ouattagouna", "Tin-Akoff", "Ber", "Banikane", "Ansongo",
"Bourem", "Gao", "Tarkint", "Téméra", "Tessit", "N'tillit",
"Bourra", "Bara")), row.names = c("2327", "4748", "7169",
"14432", "16853", "19274", "21695", "24116", "36192", "38642",
"41063", "43484", "45905", "48326", "53168", "55589", "58010",
"60431", "62852", "65273"), class = "data.frame")
ui <- pageWithSidebar(
headerPanel('Scattetrplot'),
sidebarPanel(
selectInput('xcol', 'X Variable', choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"),selected="Acled_Total",
multiple=F),
selectInput('ycol', 'Y Variable', choices = c("I3", "I4", "I5", "I6" ),selected="I3",
multiple=F),
),
mainPanel(
plotOutput('plot1')
)
)
server <- function(input, output) {
new<-reactive({
myvars <- input$xcol
newdata <- n[myvars]
})
output$plot1<-renderPlot({
ggplot(n, aes(x=input$xcol, y=input$ycol, fill = ADM0_FR, shape = ADM0_FR)) +
geom_point(position=position_jitter(h=0.1, w=0.1), alpha = 0.5, size = 1.5) +
scale_fill_manual(values=c("red","blue","orange")) +
scale_shape_manual(values= c(23, 24, 25)) +
geom_text(position=position_jitter(h=0.10, w=0.55),
aes(label= ifelse(new()[,1] > quantile(new()[,1], 0.98),
as.character(ADM3_FR),'')),vjust=3, size=2.75)+
labs(x = "",y="")+
theme_bw()
})
}
shinyApp(ui, server)
问题是您的输入只是字符串。因此,做例如x = input$xcol
意味着你在 x
aes 上映射一个常量值,即你正在做类似 aes(x = "A", y = "B", ..)
的事情
要告诉 ggplot2 您想要将名称存储在 input$xcol
中的数据列映射到美学上,您可以例如使用 rlang
中的 .data
代词,即 x = .data[[input$xcol]]
:
library(shiny)
library(ggplot2)
ui <- pageWithSidebar(
headerPanel("Scattetrplot"),
sidebarPanel(
selectInput("xcol", "X Variable",
choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"), selected = "Acled_Total",
multiple = F
),
selectInput("ycol", "Y Variable",
choices = c("I3", "I4", "I5", "I6"), selected = "I3",
multiple = F
),
),
mainPanel(
plotOutput("plot1")
)
)
server <- function(input, output) {
new <- reactive({
myvars <- input$xcol
newdata <- n[myvars]
})
output$plot1 <- renderPlot({
ggplot(n, aes(x = .data[[input$xcol]], y = .data[[input$ycol]], fill = ADM0_FR, shape = ADM0_FR)) +
geom_point(position = position_jitter(h = 0.1, w = 0.1), alpha = 0.5, size = 1.5) +
scale_fill_manual(values = c("red", "blue", "orange")) +
scale_shape_manual(values = c(23, 24, 25)) +
geom_text(
position = position_jitter(h = 0.10, w = 0.55),
aes(label = ifelse(new()[, 1] > quantile(new()[, 1], 0.98),
as.character(ADM3_FR), ""
)), vjust = 3, size = 2.75
) +
labs(x = "", y = "") +
theme_bw()
})
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:4535
我有下面这个闪亮的应用程序,我在其中创建了一个动态散点图。问题是,当我在闪亮的应用程序外部创建我的 ggplot
时,我以更大且最易读的形式获得它,但在 shiny
内部,我得到的结果非常小,两个轴上没有数字标签。为什么会这样?
app.R
library(shiny)
library(ggplot2)
n<-structure(list(ADM3_PCODE = c("ML100101", "ML100201", "ML070301",
"ML070105", "ML100301", "ML070107", "ML100401", "ML070104", "BF560105",
"ML060502", "ML060302", "ML070101", "ML070202", "ML070303", "ML070204",
"ML070205", "ML070106", "ML070305", "ML070103", "ML070102"),
Acled_Health = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0), Acled_no_inst = c(4, 0, 0, 0, 0, 0, 0,
0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0), Acled_Total = c(4,
0, 1, 0, 0, 0, 0, 0, 3, 1, 0, 2, 1, 4, 1, 0, 1, 1, 0, 0),
I3 = c(1.09376534720521, 2.98445151003512, 1.38675338665065,
2.71565793377472, 3.18930523219588, 2.90551791702655, 1.53290303794127,
1.06882818216774, 1.0422158290422, -0.278650206004393, 1.74575777732344,
1.06030283919043, 1.96049964093946, 2.63957106704249, 2.58417173100033,
2.28469585057556, 0.531106478645378, 2.52253985112635, 2.72742421204698,
0.943157796157148), I4 = c(2.97790487569313, 4.39377989487155,
4.20062305434793, 3.5875729546847, 2.87065592255387, 1.93698960334718,
2.32611240844526, 3.79504053511001, 3.26867102154575, 3.54402045486982,
2.59943279715003, 2.24078712967162, 3.7837957824595, 0.629466739192882,
2.23259745421704, 3.22143437813918, 2.832868377928, 1.85546575024811,
2.44473249718205, 1.27700101283808), I5 = c(4.50640751725631,
4.59205796716096, 2.81987303382463, 5.6939014694895, 4.84552750414595,
3.48919571832088, 3.90947133936061, 5.62442105113242, 5.46450373445726,
3.81405499661111, 3.64136973672331, 1.48012372278708, 3.45348478980314,
2.79266252795185, 2.92273131958892, 3.12803195965532, 4.8713736085137,
4.78116333258574, 5.07934714109017, 4.90711103863146), I6 = c(4.71964925827176,
7.88685736651118, 4.48033628274155, 4.63463388102439, 5.41071131184507,
4.49985156270955, 4.81313128120664, 5.16036902503232, 4.07947495928142,
3.76143006251051, 5.18794193926347, 5.60747967555871, 5.50504418957362,
2.85716269808936, 7.16407719804625, 4.77737451764469, 5.7199718544193,
6.27105214031155, 3.48487832187209, 5.00715544299671), ADM0_FR = c("Mali",
"Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Burkina Faso",
"Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali", "Mali",
"Mali", "Mali", "Mali"), ADM3_FR = c("Ménaka", "Andéramboukane",
"Anchawadi", "Talataye", "Inékar", "Tin Hama", "Tidermène",
"Ouattagouna", "Tin-Akoff", "Ber", "Banikane", "Ansongo",
"Bourem", "Gao", "Tarkint", "Téméra", "Tessit", "N'tillit",
"Bourra", "Bara")), row.names = c("2327", "4748", "7169",
"14432", "16853", "19274", "21695", "24116", "36192", "38642",
"41063", "43484", "45905", "48326", "53168", "55589", "58010",
"60431", "62852", "65273"), class = "data.frame")
ui <- pageWithSidebar(
headerPanel('Scattetrplot'),
sidebarPanel(
selectInput('xcol', 'X Variable', choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"),selected="Acled_Total",
multiple=F),
selectInput('ycol', 'Y Variable', choices = c("I3", "I4", "I5", "I6" ),selected="I3",
multiple=F),
),
mainPanel(
plotOutput('plot1')
)
)
server <- function(input, output) {
new<-reactive({
myvars <- input$xcol
newdata <- n[myvars]
})
output$plot1<-renderPlot({
ggplot(n, aes(x=input$xcol, y=input$ycol, fill = ADM0_FR, shape = ADM0_FR)) +
geom_point(position=position_jitter(h=0.1, w=0.1), alpha = 0.5, size = 1.5) +
scale_fill_manual(values=c("red","blue","orange")) +
scale_shape_manual(values= c(23, 24, 25)) +
geom_text(position=position_jitter(h=0.10, w=0.55),
aes(label= ifelse(new()[,1] > quantile(new()[,1], 0.98),
as.character(ADM3_FR),'')),vjust=3, size=2.75)+
labs(x = "",y="")+
theme_bw()
})
}
shinyApp(ui, server)
问题是您的输入只是字符串。因此,做例如x = input$xcol
意味着你在 x
aes 上映射一个常量值,即你正在做类似 aes(x = "A", y = "B", ..)
要告诉 ggplot2 您想要将名称存储在 input$xcol
中的数据列映射到美学上,您可以例如使用 rlang
中的 .data
代词,即 x = .data[[input$xcol]]
:
library(shiny)
library(ggplot2)
ui <- pageWithSidebar(
headerPanel("Scattetrplot"),
sidebarPanel(
selectInput("xcol", "X Variable",
choices = c("Acled_Total", "Acled_no_inst", "Acled_Health"), selected = "Acled_Total",
multiple = F
),
selectInput("ycol", "Y Variable",
choices = c("I3", "I4", "I5", "I6"), selected = "I3",
multiple = F
),
),
mainPanel(
plotOutput("plot1")
)
)
server <- function(input, output) {
new <- reactive({
myvars <- input$xcol
newdata <- n[myvars]
})
output$plot1 <- renderPlot({
ggplot(n, aes(x = .data[[input$xcol]], y = .data[[input$ycol]], fill = ADM0_FR, shape = ADM0_FR)) +
geom_point(position = position_jitter(h = 0.1, w = 0.1), alpha = 0.5, size = 1.5) +
scale_fill_manual(values = c("red", "blue", "orange")) +
scale_shape_manual(values = c(23, 24, 25)) +
geom_text(
position = position_jitter(h = 0.10, w = 0.55),
aes(label = ifelse(new()[, 1] > quantile(new()[, 1], 0.98),
as.character(ADM3_FR), ""
)), vjust = 3, size = 2.75
) +
labs(x = "", y = "") +
theme_bw()
})
}
shinyApp(ui, server)
#>
#> Listening on http://127.0.0.1:4535