Reputation: 21
I´m trying to create a plot that shows all main categories and their summed up pledged amount in million USD. Therefore I took a Kickstarter data frame, put the pledged amount usd_pledged as x-value and the category main_category as the y-value.
To make it more appealing I used fill = main_category
. After that some labelling took place labs()
,theme_classic()
, theme()
,guides()
and scale_x_continous()
Here is my code:
ggplot(Kickstarter, aes(usd_pledged/1000000,
main_category,
fill = main_category)) +
geom_histogram(stat = "identity") +
labs(x="pledged amount in mio USD", y = "main categories") +
theme_classic() +
theme(legend.position="right") +
guides(fill=guide_legend(title="categories")) +
scale_x_continuous(expand = c(0,0))
The result ist: basic-plot_Kickstarter
Now I´d like to plot this graph in a descending order.
I tried using reorder()
to get a descending order:
ggplot(Kickstarter, aes(usd_pledged/1000000,
reorder(main_category, +usd_pledged),
fill = main_category)) +
geom_histogram(stat = "identity") +
labs(x="pledged amount in mio USD", y = "main categories") +
theme_classic() +
theme(legend.position="right") +
guides(fill=guide_legend(title="categories")) +
scale_x_continuous(expand = c(0,0))
which only kind of sorts it, as the result is: halfway sorted plot_Kickstarter
Is there a way to order it correctly? (Games->Design->Technology->Film&Video->Music->Publishing->Food->Fashion->Art->Comics->Theater->Photography->Dance->Journalism->Crafts)
If you need more info, just let me know. This is my first question here :)
head()
# A tibble: 6 x 20
ID name category main_category currency deadline goal
<dbl> <chr> <chr> <chr> <chr> <date> <dbl>
1 1000003930 Gree~ Narrati~ Film & Video USD 2017-11-01 30000
2 1000004038 Wher~ Narrati~ Film & Video USD 2013-02-26 45000
3 1000007540 Tosh~ Music Music USD 2012-04-16 5000
4 1000014025 Mona~ Restaur~ Food USD 2016-04-01 50000
5 1000030581 Chas~ Drinks Food USD 2016-03-17 25000
6 100004721 Of J~ Nonfict~ Publishing CAD 2013-10-09 2500
# ... with 13 more variables: launched <chr>, pledged <dbl>,
# state <chr>, backers <dbl>, country <chr>, usd_pledged <dbl>,
# usd_pledged_real <dbl>, usd_goal_real <dbl>, percent <dbl>,
# launch_year <chr>, runtime <dbl>, state_dv <dbl>, Group <chr>
UPDATE
After trying out
Kickstarter %>%
mutate(main_category = fct_reorder(main_category, usd_pledged)) %>%
ggplot(aes(x=main_category, y=usd_pledged, fill=main_category)) +
geom_col()+
coord_flip()
it still remains in chaos, as you can see here:
Upvotes: 2
Views: 4101
Reputation: 79271
Update: I added some fake values for usd_pledged
since the column is not reconstructible:
library(forcats)
library(ggplot2)
library(dplyr)
df %>%
mutate(main_category = fct_reorder(main_category, usd_pledged)) %>%
ggplot(aes(x=main_category, y=usd_pledged, fill=main_category)) +
geom_col()+
coord_flip()
First answer:
I am not sure but I think you need geom_col
or geom_bar
Here is an example with the mtcars dataset:
mtcars
to get cars
column and mpg
fct_reorder
from forcats to order cars
by mpg
geom_col
coord_flip()
library(forcats)
library(ggplot2)
library(dplyr)
mtcars %>%
rownames_to_column("cars") %>%
select(cars, mpg) %>%
mutate(cars = fct_reorder(cars, mpg)) %>%
ggplot(aes(cars, mpg, fill=cars)) +
geom_col()+
coord_flip()
Upvotes: 3