2017-05-09 4 views
1

Ich habe ein kleines shinyapp für das Binning in Histogrammen erstellt.Dichteplot mit Datenpunkten in ggplot/Shinyapp

Sie werden in meinen Code zwei Versionen sehen:

  • die uncomment eines mit ggplot
  • und dem Kommentar mit DensityPlot unter.

In meinem DensityPlot werden die Datenpunkte korrekt dargestellt werden, aber in meinem ggplot ich weiß nicht, wie ich die gleiche Wirkung erkennen kann ... Ich habe versucht, es mit geom_point, aber ich denke, ein Fehler mit dem ist y?

also wie kann ich die data_points in meinem ggplot richtig plotten?

es so viel schöner als DensityPlot :-) sieht zhanks viel für jede Lösung ...

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) 

Antwort

0

Versuchen Sie, die geom_point() Element ersetzt durch:

geom_jitter(data=size_df,aes(x=size,y=0.01), width=0.02,height = 0.02) 
+0

Dank viel. ..geom_jitter nicht geom_point * Daumen hoch –

Verwandte Themen