我构建了一个R闪亮的应用程序,该应用程序绘制了两个ggplot2图。用户可以放大和缩小图。但是,数据在y轴上彼此非常接近(比例尺以米为单位),并且y轴标签当前未在y轴上显示小数位,如下面的屏幕截图所示。
我尝试根据以下SO答案更改小数精度:ggplot2 y axis label decimal precision,但是在闪亮的环境中什么也没有发生。此解决方案结合使用ggplot2s scale_continuous参数,提出了函数fmt_decimals:
fmt_decimals <- function(decimals=0){
function(x) format(x,nsmall = decimals,scientific = FALSE)
}
labels_dec1 <- fmt_decimals(1)
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p + scale_y_continuous(labels = labels_dec1)
我在下面的代码中附加了我的应用程序的简化版。如何在R Shiny应用程序内的ggplot2图中控制小数的精度?
df <- list(`0` = structure(data.frame(Date = structure(c(17737, 17738, 17739, 17740, 17741, 17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17922, 17924), class = "Date"),
X = c(2791162.3550637, 2791162.35592537, 2791162.34294051, 2791162.34864573, 2791162.34433394, 2791162.34740308, 2791162.34313007, 2791162.34487983, 2791162.34646726, 2791162.34200201, 2791162.34749474, 2791162.34255676, 2791162.34304754, 2791162.3425003, 2791162.34330067, 2791162.34278867, 2791162.34246682, 2791162.3438409, 2791162.34466809, 2791162.34875856, 2791162.34477708, 2791162.34662692, 2791162.34633941, 2791162.34701495, 2791162.3437676, 2791162.34370942, 2791162.34553501, 2791162.34325701, 2791162.34759088, 2791162.34038494, 2791162.33862812, 2791162.33991438, 2791162.34720632, 2791162.34631027, 2791164.24372202, 2791162.34461334, 2791162.34142775, 2791162.33764027, 2791162.33759566, 2791162.34250817, 2791162.33088489, 2791162.33793524, 2791162.3389292, 2791162.33744799, 2791162.33913297, 2791162.34362382, 2791162.33397091, 2791162.33889572, 2791162.3420609, 2791162.34256534, 2791162.34295388, 2791162.34360599, 2791162.33906351, 2791162.3410493, 2791162.34179139, 2791162.33807938, 2791162.34030308, 2791162.33740247, 2791162.33686627, 2791162.33465613, 2791162.33364715, 2791162.33327762, 2791162.33233307, 2791162.33092723, 2791162.32979484, 2791162.33032923, 2791162.32840836, 2791160.1523217, 2791162.33247584, 2791162.3277548, 2791162.32251429, 2791162.32674239, 2791162.33025114, 2791162.32962545, 2791162.32878667, 2791162.32817898, 2791162.32769788, 2791162.32821961, 2791162.32480343, 2791162.31161777, 2791162.32399605, 2791162.32336013, 2791162.32222764, 2791162.32393696, 2791162.3249891, 2791162.32406214, 2791162.32114112, 2791162.32277927, 2791162.32680753, 2791162.32359466, 2791162.31715986, 2791162.43273995, 2791162.43609669, 2791162.43228038, 2791162.43026514, 2791162.43019254, 2791162.42065433, 2791165.78288008, 2791162.42082946, 2791162.42138195, 2791162.41389611, 2791162.41563357, 2791162.41845497, 2791162.41773703, 2791162.41787637, 2791162.4187504, 2791162.41651641, 2791162.4153813, 2791162.41653668, 2791162.41481415, 2791162.41964469, 2791162.41477361, 2791162.41328494, 2791162.41301816, 2791162.41301417, 2791162.4045612, 2791162.40596568, 2791162.38755189, 2791162.39311443, 2791162.40093757, 2791162.38179792, 2791162.38089839, 2791162.38206784, 2791162.38178915, 2791162.3808269, 2791162.38363636, 2791162.38168147, 2791162.38247946, 2791162.38038856),
Y = c(1152645.1057075, 1152645.10716654, 1152645.0986932, 1152645.10973225, 1152645.10268387, 1152645.10860166, 1152645.10285369, 1152645.10415144, 1152645.10949053, 1152645.10592819, 1152645.11141594, 1152645.10374256, 1152645.10746764, 1152645.1063395, 1152645.10800735, 1152645.10815158, 1152645.10766793, 1152645.10480103, 1152645.11599135, 1152645.11151243, 1152645.10463071, 1152645.10960652, 1152645.10975319, 1152645.11164281, 1152645.11013769, 1152645.11160747, 1152645.11252802, 1152645.10673275, 1152645.11099244, 1152645.10262804, 1152645.10838721, 1152645.11149681, 1152645.11408256,1152645.11280042, 1152644.37773542, 1152645.11690802, 1152645.11770136, 1152645.1125467, 1152645.11574864, 1152645.11438099, 1152645.12331471, 1152645.1152969, 1152645.11450451, 1152645.11354352, 1152645.11153381, 1152645.11427941, 1152645.11192852, 1152645.11176822, 1152645.12555403, 1152645.12672117, 1152645.12572363, 1152645.12744387, 1152645.12433327, 1152645.12610127, 1152645.12706131, 1152645.12156846, 1152645.12355019, 1152645.12250923, 1152645.11961065, 1152645.11727393, 1152645.12012378, 1152645.1200921, 1152645.11721301, 1152645.11850732, 1152645.11513263, 1152645.12240832, 1152645.12009354, 1152645.187559, 1152645.12752105, 1152645.12267048, 1152645.11869222, 1152645.12166756, 1152645.12614825, 1152645.12081258, 1152645.12522161, 1152645.12731805, 1152645.1251948, 1152645.1250067, 1152645.11414578, 1152645.12608457, 1152645.11437481, 1152645.11610481, 1152645.119979, 1152645.11283147, 1152645.1149272, 1152645.11784599, 1152645.11696344, 1152645.1241658, 1152645.12738042, 1152645.12265048, 1152645.13113493, 1152644.90542479, 1152644.90531955, 1152644.9137409, 1152644.91079601, 1152644.91118295, 1152644.93155628, 1152644.85931671, 1152644.92505568, 1152644.92365586, 1152644.9062049, 1152644.9072195, 1152644.91269895, 1152644.91120019, 1152644.91528026, 1152644.91661213, 1152644.91545375, 1152644.91216822, 1152644.91436215, 1152644.91334168, 1152644.9132155, 1152644.91259903, 1152644.91009809, 1152644.90909567, 1152644.91015364, 1152644.90557376, 1152644.90806689, 1152644.94956111, 1152644.94274402, 1152644.91456334, 1152644.96352573, 1152644.95826583, 1152644.95863937, 1152644.95850146, 1152644.96287929, 1152644.9612743, 1152644.96621915, 1152644.94007565, 1152644.96446478))),
`4` = structure(data.frame(Date = structure(c(17739, 17740, 17741,17742, 17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751, 17752, 17753, 17754, 17755, 17757, 17758, 17759, 17760, 17761, 17762, 17763, 17764, 17766, 17767, 17768, 17769, 17770, 17772, 17773, 17774, 17775, 17776, 17777, 17778, 17779, 17780, 17781, 17782, 17783, 17784, 17785, 17786, 17787, 17789, 17790, 17791, 17792, 17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801, 17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810, 17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819, 17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828, 17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837, 17838, 17839, 17840, 17841, 17843, 17844, 17846, 17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17856, 17858, 17860, 17862, 17864, 17866, 17868, 17870, 17872, 17874, 17876, 17878, 17880, 17882, 17884, 17888, 17890, 17892, 17894, 17896, 17898, 17900, 17904, 17910, 17912, 17914, 17916, 17918, 17920, 17922, 17924), class = "Date"),
X = c(2790824.18374809, 2790824.18858669, 2790824.18446804, 2790824.1888521, 2790824.18384519, 2790824.18617796, 2790824.18771725, 2790824.18443985, 2790824.18748079, 2790824.18308143, 2790824.18520752, 2790824.18257565, 2790824.18457724, 2790824.18600788, 2790824.18375888, 2790824.1847667, 2790824.18760882, 2790824.19052547, 2790824.18875072, 2790824.19017255, 2790824.19245601, 2790824.19081026, 2790824.19017255, 2790824.18495934, 2790824.18864228, 2790824.18773663, 2790824.18766243, 2790824.18412512, 2790824.18165805, 2790824.18411731, 2790824.1886425, 2790824.18711752, 2790825.7650905, 2790824.1887942, 2790824.18584957, 2790824.18555477, 2790824.18344282, 2790824.18663601, 2790824.18287909, 2790824.18046056, 2790824.18249738, 2790824.18103211, 2790824.18272872, 2790824.18214583, 2790824.18129456, 2790824.18119768, 2790824.19036244, 2790824.19282126, 2790824.19331333, 2790824.19533039, 2790824.18985039, 2790824.19113479, 2790824.1932507, 2790824.18973607, 2790824.18726316, 2790824.18820932, 2790824.18617466, 2790824.18598988, 2790824.18462304, 2790824.18537468, 2790824.18511984, 2790824.1834283, 2790824.1861561, 2790824.18377319, 2790824.18455121, 2790823.77616338, 2790824.18794645, 2790824.18470526, 2790824.18367412, 2790824.18490804, 2790824.18672408, 2790824.18804839, 2790824.18506072, 2790824.18476163, 2790824.18430831, 2790824.18437524, 2790824.18377374, 2790824.17576408, 2790824.18188471, 2790824.17497523, 2790824.17361097, 2790824.17991796, 2790824.18053179, 2790824.18515782, 2790824.18380764, 2790824.18674445, 2790824.18748905, 2790824.18360762, 2790824.17562747, 2790824.17512053, 2790824.29807747, 2790824.29977056, 2790824.29613796, 2790824.29415652, 2790824.28809071, 2790824.28362793, 2790824.28864387, 2790824.28424219, 2790824.27961904, 2790824.28421589, 2790824.28469113, 2790824.28450756, 2790824.28310669, 2790824.28250442, 2790824.28508087, 2790824.28348406, 2790824.28309668, 2790824.28288763, 2790824.28368468, 2790824.28541184, 2790824.2850932, 2790824.28369521, 2790824.27851666, 2790824.28289038, 2790824.28139771, 2790824.27608701, 2790824.25548963, 2790824.2638138, 2790824.27164658, 2790824.25281592, 2790824.25130503, 2790824.25128763, 2790824.25101947, 2790824.2521007, 2790824.25603123, 2790824.25821979, 2790824.25660884, 2790824.27185727, 2790824.26655444, 2790824.25487382, 2790824.24117284, 2790824.24537759, 2790824.23911296, 2790824.23060396, 2790824.25309687, 2790824.26019989, 2790824.26512535, 2790824.2679404, 2790824.26690672, 2790824.25377196, 2790824.26113459),
Y = c(1152771.25205373, 1152771.26044974, 1152771.25753943, 1152771.25978308, 1152771.2558363, 1152771.25737316, 1152771.26221167, 1152771.26008521, 1152771.26175994, 1152771.25406342, 1152771.25888205, 1152771.25536511, 1152771.25642348, 1152771.25771622, 1152771.25637725, 1152771.25125447, 1152771.26784701, 1152771.26538719, 1152771.25912007, 1152771.26242374, 1152771.2644979, 1152771.26553601, 1152771.26377529, 1152771.26209083, 1152771.26141445, 1152771.26246472, 1152771.2604169, 1152771.26242945, 1152771.25709542, 1152771.26461715, 1152771.26770187, 1152771.26539248, 1152770.44825641, 1152771.26541274, 1152771.26600154, 1152771.26392009, 1152771.26404865, 1152771.26264474, 1152771.26493265, 1152771.25979324, 1152771.26063346, 1152771.26042017, 1152771.25733911, 1152771.25714285, 1152771.25710758, 1152771.25825841, 1152771.26857731, 1152771.26889403, 1152771.26763328, 1152771.27012269, 1152771.26569212, 1152771.26643379, 1152771.26831453, 1152771.26453117, 1152771.2659026, 1152771.26211273, 1152771.26160017, 1152771.25949032, 1152771.25648998, 1152771.25802036, 1152771.2564697, 1152771.25572519, 1152771.26994309, 1152771.25856009, 1152771.25577385, 1152771.21190108, 1152771.26022874, 1152771.25793114, 1152771.25677626, 1152771.2583099, 1152771.26039744, 1152771.25265629, 1152771.25645023, 1152771.25709743, 1152771.25675924, 1152771.25475927, 1152771.25495515, 1152771.25653707, 1152771.25558733, 1152771.24623797, 1152771.24770836, 1152771.2549661, 1152771.25507885, 1152771.25924985, 1152771.2628605, 1152771.26486285, 1152771.26706399, 1152771.26320436, 1152771.25670899, 1152771.25969191, 1152771.04282095, 1152771.0422443, 1152771.04935005, 1152771.0453424, 1152771.03681296, 1152771.04392157, 1152771.04540486, 1152771.04963515, 1152771.02783388, 1152771.03612569, 1152771.03824284, 1152771.04113289, 1152771.04169045, 1152771.04252987, 1152771.04051328, 1152771.04079713, 1152771.03747317, 1152771.03739855, 1152771.03942934, 1152771.03740992, 1152771.0364367, 1152771.03294287, 1152771.03230096, 1152771.03823554, 1152771.03634142, 1152771.03405967, 1152771.07550426, 1152771.06546547, 1152771.03697854, 1152771.08281639, 1152771.07903828, 1152771.08392586, 1152771.0829198, 1152771.08307592, 1152771.0774657, 1152771.09114143, 1152771.07792026, 1152771.05372315, 1152771.0585986, 1152771.07327235, 1152771.08565988, 1152771.08055774, 1152771.09170355, 1152771.09050087, 1152771.05223544, 1152771.05222645, 1152771.05033638, 1152771.04887938, 1152771.04816407, 1152771.05337247, 1152771.06691153))))
fmt_decimals <- function(decimals=0){
function(x) format(x,nsmall = decimals,scientific = FALSE)
}
ui <- fluidPage(
titlePanel("TestApp", windowTitle = "Test Shiny App"),
tabsetPanel(tabPanel("Tab1",
wellPanel(fluidRow(column(selectInput("Number", "Datset number:", names(df)), width = 1),
column(dateRangeInput('dateRange_df',
label = 'Select the time interval for plots',
start = "2018-07-24",
separator = " - ", format = "dd/mm/yy",
startview = 'year' , weekstart = 1), width = 4, offset = 3))),
fluidRow(column(plotOutput("plot1", height = 500, dblclick = "plot1_dblclick", brush = brushOpts(id = "plot1_brush", resetOnNew = TRUE)), width = 6),
column(plotOutput("plot2", height = 500, dblclick = "plot2_dblclick", brush = brushOpts(id = "plot2_brush", resetOnNew = TRUE)), width = 6)))))
server <- function(input,output, session){
observeEvent(input$dateRange_df, {
ranges1$x <- input$dateRange_df
ranges2$x <- input$dateRange_df
})
observeEvent(input$Number,{
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
})
# Plot1
ranges1 <- reactiveValues(x = NULL, y = NULL)
output$plot1 <- renderPlot({
if (!is.null(ranges1$x)) {
ranges1$x <- as.Date(ranges1$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec1 <- fmt_decimals(1)
p <- ggplot(df[[input$Number]], aes(x = Date, y = X)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges1$x, ylim = ranges1$y, expand =FALSE)
p + labs(x = "", y = "X [m]", title = paste("Plot1 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + scale_y_continuous(labels = labels_dec1)
p + ggtitle(paste("Plot1 - Dataset number", input$Number))
})
observeEvent(input$plot1_dblclick, {
brush <- input$plot1_brush
if (!is.null(brush)) {
ranges1$x <- c(brush$xmin, brush$xmax)
ranges1$y <- c(brush$ymin, brush$ymax)
} else {
ranges1$x <- input$dateRange_df
ranges1$y <- NULL
}
})
# Plot2
ranges2 <- reactiveValues(x = NULL, y = NULL)
output$plot2 <- renderPlot({
if (!is.null(ranges2$x)) {
ranges2$x <- as.Date(ranges2$x, origin = "1970-01-01")
}
if(input$Number == "0"){
col = rgb(165,0,38, max = 255)
}
else if(input$Number == "4"){
col = rgb(215,48,39, max = 255)
}
labels_dec2 <- fmt_decimals(1)
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
})
observeEvent(input$plot2_dblclick, {
brush <- input$plot2_brush
if (!is.null(brush)) {
ranges2$x <- c(brush$xmin, brush$xmax)
ranges2$y <- c(brush$ymin, brush$ymax)
} else {
ranges2$x <- input$dateRange_df
ranges2$y <- NULL
}
})
}
shinyApp(ui, server)
答案 0 :(得分:1)
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p + theme(panel.background = element_rect(fill = "grey99"))
p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))
应替换为
p <- ggplot(df[[input$Number]], aes(x = Date, y = Y)) +
geom_point(colour = col) + coord_cartesian(xlim = ranges2$x, ylim = ranges2$y, expand =FALSE)
p <- p + labs(x = "", y = "Y [m]", title = paste("Plot 2 - Number ", input$Number))
p <- p + theme(panel.background = element_rect(fill = "grey99"))
p <- p + scale_y_continuous(labels = labels_dec2)
p + ggtitle(paste("Plot2 - Dataset number ", input$Number))