ggplot2

How to pass a count of observations to the facet label?

…衆ロ難τιáo~ 提交于 2021-02-10 15:49:00
问题 I would like to add a count of observations in each facet to the "title" of each facet. For instance, I have this: library(tidyverse) mtcars %>% ggplot(aes(x = cyl)) + geom_bar()+ facet_wrap(~carb) and I would like to add the frequencies from the below function to each label table(mtcars$carb) 1 2 3 4 6 8 7 10 3 10 1 1 Thus the label for 1st row 1st column should be 1; n=7 the 1st row 2nd column 2; n=10 etc... 回答1: A solution using tidyverse . We can create an updated column to show the count

automatically convert polynomial to expression in ggplot2 title

喜欢而已 提交于 2021-02-10 15:40:43
问题 Here is some code I am using to auto generate some regression fits; require(ggplot2) # Prep data nPts = 200 prepared=runif(nPts,0,10) rich=5-((prepared-5)^2)/5 + 5*runif(length(prepared)) df <- data.frame(rich=rich, prepared=prepared) deg = 1 # User variable lm <- lm(df$rich ~ poly(df$prepared, deg, raw=T)) # Create expression coefs <- lm$coefficients eq <- paste0(round(coefs,2),'*x^', 0:length(coefs), collapse='+') # (1) pl <- ggplot(df, aes(x=prepared, y=rich)) + geom_point() + geom_smooth

Using geom_rect in plotly

时间秒杀一切 提交于 2021-02-10 14:56:54
问题 I have the following plot using ggplot2 and plotly . However, the plotly image does not display the shaded areas I've inserted using geom_rect . I'm guessing that this is because plotly doesn't support geom_rect , does anyone have a workaround to solve this? Here is my code library(rmsfuns) packages <- c('ggplot2','ggthemes','lubridate', 'readxl','plotly', 'dplyr','tidyr', 'scales') load_pkg(packages) min <- as.Date('2015-01-01') max <- as.Date('2020-07-01') p1 <- ggplot(pmi_data, aes(Date

Using geom_rect in plotly

∥☆過路亽.° 提交于 2021-02-10 14:55:17
问题 I have the following plot using ggplot2 and plotly . However, the plotly image does not display the shaded areas I've inserted using geom_rect . I'm guessing that this is because plotly doesn't support geom_rect , does anyone have a workaround to solve this? Here is my code library(rmsfuns) packages <- c('ggplot2','ggthemes','lubridate', 'readxl','plotly', 'dplyr','tidyr', 'scales') load_pkg(packages) min <- as.Date('2015-01-01') max <- as.Date('2020-07-01') p1 <- ggplot(pmi_data, aes(Date

Sorting of Bars-ggplot2 in shiny

你说的曾经没有我的故事 提交于 2021-02-10 14:44:37
问题 Please find the code below for a shiny App using ggplot2, I do not know how to sort them inside the server.R code. WIth the below code I am able to display the bar chart and change it but ordering the data seems to be an issue. ui.R library(shiny) library(ggplot2) shinyUI(fluidPage( column(12,offset=5, titlePanel("Template Type by Hours")), br(), h6(textOutput("text1")), fluidRow( column(4,offset=0, wellPanel( selectInput("var","Hours", choices = colnames(sum1[2:8]),selected ="hrs_0to1")))),

How to center line in ggplot2::geom_step() similar to highcharter

南楼画角 提交于 2021-02-10 14:37:56
问题 For my plot, I would like the ggplot2::geom_step() line alignment to be centered around my points, instead of aligned to the left In highcharter::hc_add_series(type = "line") there is an option called step = "center" . See my jsfiddle for the look I am going for in ggplot2 . library(ggplot2) #> Warning: package 'ggplot2' was built under R version 3.5.1 my_data <- data.frame( x = c("2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016"), y = c(35, 41, 40, 45, 56,

How to center line in ggplot2::geom_step() similar to highcharter

青春壹個敷衍的年華 提交于 2021-02-10 14:37:21
问题 For my plot, I would like the ggplot2::geom_step() line alignment to be centered around my points, instead of aligned to the left In highcharter::hc_add_series(type = "line") there is an option called step = "center" . See my jsfiddle for the look I am going for in ggplot2 . library(ggplot2) #> Warning: package 'ggplot2' was built under R version 3.5.1 my_data <- data.frame( x = c("2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12", "2016"), y = c(35, 41, 40, 45, 56,

How to make histograms in Python (scipy.stats) look as good as R?

北城余情 提交于 2021-02-10 14:17:18
问题 The following plot and its code were generated in R (source). How can I replicate this quality of a histogram in Python code using scipy.stats ? x = rgamma(1000, 3, .1) hist(x, prob=T, br=30, col="skyblue2", main="n = 1000: GAMMA(3, .1)") curve(dgamma(x, 3, .1), add=T, lwd=2, col="orange") abline(v = 55.81, lwd=2, col="blue") abline(v = 53.2232, lwd=2, col="brown", lty="dotted") The R plot above is alot better than Python's scipy.stats histograms, one example shown below, but I know there are

How to make histograms in Python (scipy.stats) look as good as R?

时间秒杀一切 提交于 2021-02-10 14:14:59
问题 The following plot and its code were generated in R (source). How can I replicate this quality of a histogram in Python code using scipy.stats ? x = rgamma(1000, 3, .1) hist(x, prob=T, br=30, col="skyblue2", main="n = 1000: GAMMA(3, .1)") curve(dgamma(x, 3, .1), add=T, lwd=2, col="orange") abline(v = 55.81, lwd=2, col="blue") abline(v = 53.2232, lwd=2, col="brown", lty="dotted") The R plot above is alot better than Python's scipy.stats histograms, one example shown below, but I know there are

R Custom Color Scale for Plotting

人盡茶涼 提交于 2021-02-10 13:29:28
问题 I am attempting to create a custom scale that will better display the data I have. For the first plot, the data was relatively uniform, falling in between 0 and 3. However, for my second dataset, the majority of the data is less than 100, and approximately 90% of the data is under 1000. (plots at bottom) The column with the values I am trying to plot is named 'data1', and the values are whole numbers ranging from 0-10000. I tried applying the code on https://gis.stackexchange.com/questions