2017-05-09 13 views
1

ヒストグラムでのビニングを示すために少しshinyappを構築しました。ggplot/Shinyappのデータポイントを使った密度プロット

あなたは私のコードの2つのバージョンが表示されます。

  • ggplot
  • と下にdensityplotとコメントしてコメントを解除1。私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) 

答えて

0

がでgeom_point()要素を交換してみてください... は、各ソリューションのために多くのことをzhanks :-) densityplotよりそんなに立派に見えます:

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

おかげでたくさん。 ..geom_jitterではないgeom_point * thumbs up –

関連する問題