Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. library(data.table)
  2. library(dplyr)
  3. library(ggplot2)
  4. library(scales)
  5.  
  6. data <-
  7. mtcars %>%
  8. mutate(name = dimnames(mtcars)[[1]])
  9.  
  10.  
  11. # bar chart with scale colour
  12. data %>%
  13. ggplot(aes(x=reorder(name,mpg), y=mpg, fill=hp)) +
  14. geom_bar(stat='identity', width=0.8) +
  15. scale_fill_continuous(high='red', low='gray90') +
  16. scale_y_continuous(expand=c(0.01,0.01)) +
  17. coord_flip() +
  18. geom_text(
  19. aes(label=paste(mpg,'m/g')),
  20. size=3,
  21. hjust=1.2, vjust=0.2,
  22. fontface='bold') +
  23. theme(
  24. panel.background = element_blank(),
  25. axis.ticks = element_blank(),
  26. axis.title = element_blank(),
  27. axis.text.x = element_blank()) +
  28. labs(
  29. x='Miles Per Gallon',
  30. fill='Horse Power',
  31. title='MPG vs HP : Trade off')
  32.  
  33. fit <- lm(hp ~ poly(mpg,2), data=data)
  34.  
  35. data %>%
  36. ggplot(aes(x=mpg, y=hp)) +
  37. geom_point() +
  38. theme_bw() +
  39. theme(
  40. panel.grid = element_blank(),
  41. axis.title.x = element_text(angle=-10, face='bold'),
  42. axis.title.y = element_text(angle=20, face='bold')) +
  43. geom_line(
  44. aes(x=mpg, y=predict(fit, data.frame(mpg))),
  45. colour='red',
  46. linetype='dashed') +
  47. labs(title='MPG vs HP : Negative Relationship')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement