![]() The cause of death which really stands out in death by hanging for males denoted by Hang on the right graph. One can easily see that male suicide counts are much higher than female suicide rates. This facet_grid output really helps in comparing the two graphs. The strip.background and strip.text attributes in theme() allows for customization of the bars associated with f and m. ~Sex) allows for separate bar graphs by sex. Strip.text = element_text(face="bold", size=rel(1.2))) Strip.background = element_rect(fill="lightgreen", colour="black", size=1), Legend.title = element_text(face="bold", size = 10), Title = "Suicide Method Data From The UK By Gender \n") We can create an alternative bar graph which includes gender on top of age group and cause of death.įacet_plot <- ggplot(suicide_data, aes(x = Cause, y = Count, fill = Age)) The above graph is very nice indeed but it does not consider gender. The theme() function allows for custom appearances such as font colours, font text and title centering.Ĭonsidering Gender With Facet Grid Bar Graph Scale_fill_discrete(labels=c(“Middle Aged”, “Old”, “Young”) allows for labeling the colours in the legend. If this scale_x_discrete() function is not there, I think it shows the x-values from the table such as drug, gun, other, etc. The scale_x_discrete() function allows for labeling my x-values in a neat way. Having position = “dodge” is crucial for the side by side bars. I have aes(x = Cause, y = Count, fill = Age) for Cause of Death on the bottom, Count for the bar lengths and fill = Age for the different bar colours. Here are some notes about the code and the graph above: Legend.title = element_text(face="bold", size = 10)) Theme(plot.title = element_text(hjust = 0.5),Ī = element_text(face="bold", colour="blue", size = 12),Ī = element_text(face="bold", colour="blue", size = 12), Scale_fill_discrete(labels=c("Middle Aged", "Old", "Young")) Title = "Suicide Method Data From The UK Results \n") Labs(x = "\n Cause Of Death", y = "Count \n", fill = "Age Group \n", Geom_bar(stat = "identity", position = "dodge") Ggplot(suicide_data, aes(x = Cause, y = Count, fill = Age)) # Bar Plot (x axis for Cause, y axis for Counts, colours for Age): This code allows for the creation of the bar graph visual below. I wanted my bar graph to look at the cause of death, the counts associated with the cause of death and I wanted to put all the three age groups in it too. You can rename column titles using colnames() in R.Ĭolnames(suicide_data) <- c("Count", "Cause", "Age", "Sex") ![]() It provides a description and detailed information of the suicide dataset.įrom head() and tail(), you can see that the column titles are not the greatest. The screenshot image below is from page 97 of the documentation of the faraway package. # 36 10 other o f # Check structure of data: Head(suicide_data) tail(suicide_data) # y cause age sex I then preview the data and check the data structure using head(), tail() and str() respectively. I save the data suicide under the variable suicide_data. The lines of code below shows the loading of the ggplot2 and faraway libraries.įrom the faraway package the suicide dataset is under the name suicide. This particular dataset can be found in the faraway library in R. Geom_text(aes(y=label_ypos, label=len), vjust=1.For this example, I have chosen a dataset with the topic of suicide. If you want to place the labels at the middle of bars, you have to modify the cumulative sum as follow : df_cumsum <- ddply(df_sorted, "dose", Geom_text(aes(y=label_ypos, label=len), vjust=1.6, Ggplot(data=df_cumsum, aes(x=dose, y=len, fill=supp)) Head(df_cumsum) # supp dose len label_ypos # 6 VC D2 33.0 # Calculate the cumulative sum of len for each dose Calculate the cumulative sum of the variable len for each dose. ![]() Sort the data by dose and supp : the package plyr is used.Position = position_dodge(0.9), size=3.5) Īdd labels to a stacked barplot : 3 steps are required Geom_text(aes(label=len), vjust=1.6, color="white", ![]() Geom_bar(stat="identity", position=position_dodge()) Add labels to a dodged barplot : ggplot(data=df2, aes(x=dose, y=len, fill=supp))
0 Comments
Leave a Reply. |