How can I plot the histogram below, using ggplot (R) and/or matplotlib (Python)?

In base plot I use:

histogram(~ Wrkday | Year+Avg, data=Data, layout=c(3,2))

graph by base plot

The data I work on:

enter image description here

Code in R:

Input <- ("
Year       Student  Wrkday
      '1st year'  a        1200
      '1st year'  b        1400
      '1st year'  c        1350
      '1st year'  d         950
      '1st year'  e        1400
      '1st year'  f        1150
      '1st year'  g        1300
      '1st year'  h        1325
      '1st year'  i        1425
      '1st year'  j        1500
      '1st year'  k        1250
      '1st year'  l        1150
      '1st year'  m         950
      '1st year'  n        1150
      '1st year'  o        1600
      '1st year'  p        1300
      '1st year'  q        1050
      '1st year'  r        1300
      '1st year'  s        1700
      '1st year'  t        1300
      '2nd year'  u        1100
      '2nd year'  v        1200
      '2nd year'  w        1250
      '2nd year'  x        1050
      '2nd year'  y        1200
      '2nd year'  z        1250
      '2nd year'  aa       1350
      '2nd year'  ab       1350
      '2nd year'  ac       1325
      '2nd year'  ad       1525
      '2nd year'  ae       1225
      '2nd year'  af       1125
      '2nd year'  ag       1000
      '2nd year'  ah       1125
      '2nd year'  ai       1400
      '2nd year'  aj       1200
      '2nd year'  ak       1150
      '2nd year'  al       1400
      '2nd year'  am       1500
      '2nd year'  an       1200
      '3rd year'  u        1600
      '3rd year'  v        1700
      '3rd year'  w        1450
      '3rd year'  x        1650
      '3rd year'  y        1800
      '3rd year'  z        1550
      '3rd year'  aa       1950
      '3rd year'  ab       1750
      '3rd year'  ac       1925
      '3rd year'  ad       1825
      '3rd year'  ae       1625
      '3rd year'  af       1525
      '3rd year'  ag       1800
      '3rd year'  ah       1725
      '3rd year'  ai       1200
      '3rd year'  aj       1600
      '3rd year'  ak       1950
      '3rd year'  al       1100
      '3rd year'  am       1400
      '3rd year'  an       1600

Data <- read.table(textConnection(Input),header=TRUE)
los<-rbinom(nrow(Data), size = 1, prob=0.7)
Data$Avg<-ifelse(los==1,"Above 4.0","Below 4.0")

I would like to plot linear model in each box instead of histogram.

  • 1
    $\begingroup$ Do you want to create a linear model? What are you trying to do? Some example data would be good. $\endgroup$ Jan 7 '19 at 21:52
  • $\begingroup$ @wl1234, I have included a fraction of the dataset $\endgroup$
    – Learner132
    Jan 8 '19 at 18:05
  • $\begingroup$ @Learner132 I updated my answer given your comment. $\endgroup$ Jan 8 '19 at 19:13
  • $\begingroup$ Thanks, but please post data and not images of data. See: stackoverflow.com/questions/5963269/… $\endgroup$ Jan 8 '19 at 19:55
  • 1
    $\begingroup$ @wl1234: updated $\endgroup$
    – Learner132
    Jan 8 '19 at 20:09

I don't know what you mean by "with linear model" in the title, but here's code that generates a toy dataset and replicates your plot.

x<-crossing(year=paste("Year", 1:3), avg=c("Above 4.0", "Below 4.0"))
x$dat<-replicate(6, tibble(wrkday=runif(100, 1000, 2000)))
x %>% 
  unnest(dat) %>% 
  ggplot(aes(dat)) + 
  geom_histogram(aes(y=..ncount..), bins=20) + 

enter image description here


Given your comment I think I understand what you mean by "with linear model." You want to see how wrkday varies as a function of year, faceted by avg. Here's some code:


x<-tibble(year=sample(factor(paste("Year", 1:3)), 600, replace=T), 
          avg=sample(c("Above 4.0", "Below 4.0"), 600, replace=T)) %>%
  mutate(wrkday=rnorm(600, mean=1000*(as.integer(year)-1)/2, sd=300))

ggplot(x, aes(x=as.integer(year), y=wrkday)) + 
  geom_jitter(width=0.2) + 
  geom_smooth(method="lm") + 

enter image description here

  • $\begingroup$ Thanks, you are really helpful, I have learned new things $\endgroup$
    – Learner132
    Jan 8 '19 at 18:08
  • $\begingroup$ Actually, the goal is to create and plot a series of linear model with wrkday as function of year/avg then both of them as well as their interaction for each year and avg. Still your code kind of giving me some new perspective. $\endgroup$
    – Learner132
    Jan 8 '19 at 20:20
  • $\begingroup$ Well, maybe you can figure it out from here. If not, perhaps you can sketch what the chart should look like and post of photo of that. Then I'm pretty sure I can tell you how to produce it. $\endgroup$ Jan 8 '19 at 20:52
  • 1
    $\begingroup$ Using geom_smooth(method ='lm', formula = y~x) get me there. Thank you $\endgroup$
    – Learner132
    Jan 9 '19 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.