How to create a stacked histogram with percentages, so that the fill variable is the percentage of the x-variables percentage? - percentage

I need to do a bar chart, or histogram, including 3 variables.
Variable "Block" shall be in the facet_grid option.
On the x-axis shall be the variable H18SkaBet. And on the y-axis H18Skada.
The bins shall represent in total H18 SkaBet of each Block, and stacked with H18Skada. It should be basically a graph as the code underneath, just in percent.
ggplot(ThuSve, aes(H18SkaBet, fill = factor(H18Skada), colors= H18Skada)) +
geom_histogram(binwidth = 0.5)+
facet_grid(~Block, labeller=labeller(Block = labels))+
scale_x_continuous((name="Damage severity"))+
scale_y_continuous(name="Number of seedlings")+
theme_dark()+
scale_fill_manual(values = c("#CCFF99","#CCCCCC","#CCFFFF","#CC9966","#33CC99", "#339900", "#FF0033", "#666666"),
name ="Damage cause",
breaks=c("0", "1","2","3", "4", "5", "6","7","8", "9"),
labels=c("No Damage", "Fungus","Frost", "Drought", "Winter dessication", "Vegetation", "Game","Insects", "Pine Weevil", "Other/Unknown"),
aesthetics = "fill")

Difficult to answer without any data.
But if you want percentage values, maybe this link can be useful :
How to plot a 'percentage plot' with ggplot2.

Related

Stacked Bargraph showing Percentage

Currently i have plotted a stacked bargraph to display the amount of people having delays in each month with the following code
q1%>%
mutate(DepDelay1 =factor(x=DepDelay1, levels=c(0,1), labels=c("No","Yes"))) %>%
filter(Year==2005)%>%
ggplot(aes(x=factor(Month), fill=DepDelay1)) +
geom_bar(position="stack") +
ggtitle("DepDelay of Flight") +
guides(fill=guide_legend("Delay")) +
xlab ("Month")+
ylab ("Flight Count")+
geom_text(aes(label=..count..),
stat='count',
colour = "white",
size = 2.5,
position = position_stack(vjust = 0.5))
Graph Example for position="stack"
However i cannot seem to produce a 100% stacked bargraph after changing from
geom_bar(position="stack")
to
geom_bar(position="fill")
Graph Example for position="fill"
This method works before i adjust the geom_text and adding the filter to my current code.
Anyone know what's the issue ? From what i understand, a Y-variable is needed however i don't have a suitable value to input it from the dataset and i'm trying to plot the count per month.

ggplot legend with long text

I am trying to add more descriptive text to my current legend, but as soon as it exceeds about 15 characters it drops off the graph and the legend. Is there a setting to exceed the text limit when creating a ggplot legend?
p +
scale_fill_manual(breaks=c("Game Break",
"gameLose_noCash",
"gameWin", "Songs Score =0", "Songs Score =NA",
"Songs Score >0, popular", "Songs Score >0, trend (and popular)"),
values=c("black", "red", "gold", "steelblue1", "light grey", "seagreen2", "seagreen4"))
The moment I add more text to the above breaks, it doesn't show up.
Any help is appreciated!

Is there a way to alter the amount of significant digits displaying as a bar chart data label on a calculated field in ggplot?

I am creating a bar chart through ggplot to display survey responses. I want to add data labels, but when I add a data label it displays 14 digits after the decimal. I do not need any digits after the decimal. Where in my code can I have ggplot round the y values?
I have tried several other code suggestions that I have found online, but because this is a calculated field through the below code, the percentages are not displayed in my imported data, the imported data only has the survey responses and I am dropping them into the displayed categories.
ggplot(activeanglers, aes(factor(YearsFishing))) +
geom_bar(aes(fill = factor(..x..)), stat="count") +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
ylab("relative frequencies") + xlab("How many years have you fished?") + ylab("Percent of Respondents") + scale_x_discrete(breaks=c("1","2","3", "4", "5", "NA"),labels=c("First Year", "2-5", "6-9", "10+", "Missing", "NA")) + theme(legend.position = "none") + geom_text(aes(label=stat(prop)*100, group=1),stat='count',nudge_y=0.125)

How to add x-coordinates on x axis in a bar plot using ggplot?

I am making a plot of the distribution of simulated streak lengths of the independent shooter. The x axis is length (0, 1, 2, 3, etc. each stands for a different situation) and y axis is count (how many times). Here is my simple code:
ggplot(data = sim_streak, aes(x = length)) + geom_bar()
The bar plot shows as below:
enter image description here
But the x-coordinates are only "0", "2", "4", "6", "8". How could I add more x-coordinates on x axis, like "0", "1", "2", "3", "4", "5", "6", "7","8"?
At beginning, I thought it was because my plots area in Rstudio was not big enough to accommodate all the x-coordinates. But it did not work even though I enlarge the size of the layout of the pane.
Then, I tried to search and tested whether scale_x_continuous () worked or not. Unfortunately I failed.
Look forward to your suggestions. Thank you.
You can try changing your x variable to a factor, then plotting. Use + xlab() to change the x-axis title.
data.frame(x=1:10,y=runif(10)) %>% ggplot(aes(factor(x),y)) +
geom_bar(stat='identity') + xlab('situation number')
As Kstew pointed out, you probably want to make a vector with the right names for the groups you are plotting. But if you need to change them by hand you could use the following code:
library(ggplot2)
x <- c("group 1", "group 2")
y <- c(10,15)
ggplot() +
geom_bar(aes(x = x, y = y), stat = "identity") +
scale_x_discrete(labels = c("Group A", "Group B"))
I however do not recommend doing this as it is error prone.
GGplot barplot with hand drawn names
I have updated the code as
ggplot(data = sim_streak, aes(x = factor(length))) + geom_bar() + xlab('length')
And the plot shows as:
Without the "xlab('length')", the x-axis label will be "factor(length)". Thank you for your suggestions.

Stacked bar charts ggplot2 - reorder fill

I'm plotting a stacked bar chart using ggplot2. I've created a dataframe (df_car) with the variables car_make, color and proportion, the latter being numerical. There are 20 types of car_make which go along the x-axis, and 4 of color which go as fills. The proportion for each car_make adds up to 1.
I didn't want the car_make in alphabetical order so I re-ordered it:
df_car$car_make <- factor(df_car$car_make, levels = c("toyota", "ford", "mercedes", etc.)
Then I re-ordered the fill levels:
df_car$color <- factor(df_car$color, levels = c("red", "white", "black", "silver")
I plot the stacked bar plot:
bp_car<- ggplot(df_car, aes(x=car_make, y=proportion, fill=color)) + geom_bar(stat="identity")
The x-axis comes out as I specified. But the order of the bar fills remains alphabetical...only the order of the legend responds and comes out as specified. Performing...
levels(df_car$color)
gives...
"red", "white", "black", "silver"
How can I get the bar fills to re-order?
You don't need a data column with color names in it (ggplot is different from base graphics in this way). Once your car levels are ordered appropriately (as you've done), you need to set a fill color scale
bp_car <- ggplot(df_car, aes(x = car_make, y = proportion, fill = car_make)) +
geom_bar(stat="identity") +
scale_fill_manual(values = c("red", "white", "black", "silver"))

Resources