密度图,数据点在ggplot / Shinyapp中

时间:2017-05-09 09:26:49

标签: r ggplot2 shiny density-plot

我建立了一个shinyapp来展示直方图中的Binning。

您将在我的代码中看到两个版本:

  • 使用 ggplot
  • 取消注释
  • 以及 densityplot 下方的评论。

在我的密度图中,数据点将被正确绘制,但在我的ggplot中,我不知道如何实现相同的效果...我用geom_point尝试了它,但我认为y有一个错误?

那么如何才能正确绘制ggplot中的data_points?

它看起来比密度图更好:-) 对每个解决方案都有很多感谢......

library(ggplot2)
library(lattice)
library(plyr)
library(DT)

ui <- fluidPage(

        # Plot
        plotOutput("plot"),

        br(),
        hr(),

        fluidRow(
          column(6,
                 sliderInput("shift", "Shift of bins", min = 0, max = 10, step = 1, value = 0)
          ),

          column(6,
                 sliderInput("binwidth", "bin width in cm", min = 0.5, max = 10, step = 1, value=3)
          )
        ),

        # table plot

        br(),
        dataTableOutput('mytable'),
        br()
  )


server <- function(input, output) {

  size1    <- c(173, 175, 175, 177, 178, 175, 176, 175, 175, 
                   178, 179, 178, 176, 177, 178, 176, 175, 184, 
                   186, 180, 182, 170, 180, 181, 183, 187)

  size2    <- c(176, 178, 183, 180, 186, 178, 175)

  zug         <- c(rep(2, length(size2)), rep(1, length(size1)))

  size_df  <- data.frame(size=c(size2, size1), zug=zug)


  # Tabelle sorted
  output$mytable = renderDataTable({
    count(size_df)
  }, options = list(orderClasses = TRUE, lengthMenu = c(5, 10, 20, 50), pageLength = 20,
                    initComplete = JS(
                      "function(settings, json) {",
                      "$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
                      "}")))

  # Plot drawing
  output$plot <- renderPlot({

bins <- seq(165 + input$shift, 192 + input$shift, by = input$binwidth)

p1 <- ggplot(data = size_df, aes(x=size, zug))                                      +
      xlim(160, 200)                                                                      +
      ylim(0, 0.2)                                                                        +

      geom_histogram(aes(y=..density.., fill=..count..), breaks=bins, col = "white")      +
      scale_fill_gradient("number of heights", low = "red", high = "green")               +
      geom_density(alpha=.1, fill="blue", colour= "white")                                +

      # Datapoints
      geom_point(data = size_df, 
                 aes(x = size, colour = "point"),
                 y=0.1,
                 size = 2
                 )                                                                        +

      #mean
      geom_vline(aes(xintercept=mean(size, na.rm=T), colour = "mean"),
                       linetype="dashed", size=1)                                         +
                        # blank, solid, dashed, dotted, dotdash, longdash, twodash
      #median
      geom_vline(aes(xintercept=median(size, na.rm=T), colour = "median"),
                       linetype="longdash", size=1)                                       +
                        # blank, solid, dashed, dotted, dotdash, longdash, twodash
      scale_x_continuous(limits=c(165,195))                                               +
      scale_colour_manual("legend", 
                          values = c("mean" = "blue", 
                                     "median" = "red", 
                                     "point" = "black"))                                  +
      labs(x="estimated height", y="density")                               +
      theme_grey()                                                                        +
      theme(axis.text  = element_text(colour = "black", size     = rel(1.2)),
                  axis.title = element_text(size=14,face="plain"),
                                                    # "plain", "italic", "bold", "bold.italic"
                  axis.line  = element_line(arrow = arrow(angle  = 12,
                                                          length = unit(0.22, "inches"),
                                                          ends   = "last",  # "last", "first", or "both"
                                                          type   = "closed" # "open" or "closed"
                  ))
            )



# p1 <- densityplot(size_df$size, jitter=0.01,
#                   ylab="density", xlab="estimated height",
#                   panel=function(x,...){
#                   panel.histogram(x,breaks=bins, col = "lightgrey")
#                   panel.densityplot(x,...)
#                   panel.abline(v=mean(x), col.line="red")
#                   panel.abline(v=median(x), col.line="green")
#                 })

p1


  })
}

shinyApp(ui=ui, server = server)

1 个答案:

答案 0 :(得分:0)

尝试通过以下方式替换geom_point()元素:

libepic5.1.so