Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- library(dplyr)
- library(ggplot2)
- library(scales)
- data <-
- mtcars %>%
- mutate(name = dimnames(mtcars)[[1]])
- # bar chart with scale colour
- data %>%
- ggplot(aes(x=reorder(name,mpg), y=mpg, fill=hp)) +
- geom_bar(stat='identity', width=0.8) +
- scale_fill_continuous(high='red', low='gray90') +
- scale_y_continuous(expand=c(0.01,0.01)) +
- coord_flip() +
- geom_text(
- aes(label=paste(mpg,'m/g')),
- size=3,
- hjust=1.2, vjust=0.2,
- fontface='bold') +
- theme(
- panel.background = element_blank(),
- axis.ticks = element_blank(),
- axis.title = element_blank(),
- axis.text.x = element_blank()) +
- labs(
- x='Miles Per Gallon',
- fill='Horse Power',
- title='MPG vs HP : Trade off')
- fit <- lm(hp ~ poly(mpg,2), data=data)
- data %>%
- ggplot(aes(x=mpg, y=hp)) +
- geom_point() +
- theme_bw() +
- theme(
- panel.grid = element_blank(),
- axis.title.x = element_text(angle=-10, face='bold'),
- axis.title.y = element_text(angle=20, face='bold')) +
- geom_line(
- aes(x=mpg, y=predict(fit, data.frame(mpg))),
- colour='red',
- linetype='dashed') +
- labs(title='MPG vs HP : Negative Relationship')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement