SimpleNEasy
SimpleNEasy

Reputation: 889

ggplot: better presentation of barplot

I have a small data frame DF which consists of two columns X=Type, Y=Cost. want to graph a barplot for each type with its cost. I have managed to do that, however, I'm seeking a better presentation in barplot. I have three issues which I think will satisfy my requirements:

1) Since the X-axis text for each type is long, I made them with 45 degree. I tried abbreviation, it was unreadable !!!

2) Instead of the color, I was trying to use filling patterns/texture in ggplot, which turns out not possible by Hadley : fill patterns

Is there any way to make the plot readable in case of black/white printing ?

3) I'm wondering if there is a way to focus on one of the "Type" categories i.e. make it bold and special color to attract the eye to this special type. For example, I want to make the "other" result looks different from other.

I tried my thoughts, however, I'm totally open to re-design the graph. Any suggestions

Here is the data- I have used dput command:

structure(list(Type = structure(c(6L, 8L, 7L, 9L, 10L, 15L, 11L, 
17L, 3L, 16L, 5L, 19L, 4L, 14L, 2L, 18L, 13L, 1L, 12L), .Label = c("Backup Hardware ", 
"data or network control", "Email exchange server/policy", "Instant messaging control", 
"Login/system administrators/privilage", "Machine A", "Machine A with Software and   Camera", 
 "Machine A without Software", "Machine B", "Machine B without RAM and CD ROM", 
"Managment analyses software ", "Other", "Password and security", 
"public web application availability", "Software for backup", 
"System access by employees ", "Telecom and Harware", "Web site update", 
"wireless network access ponits"), class = "factor"), Cost = structure(c(4L, 
3L, 15L, 13L, 11L, 7L, 2L, 1L, 19L, 16L, 14L, 12L, 10L, 9L, 8L, 
6L, 5L, 17L, 18L), .Label = c("$1,292,312", "$1,888,810", "$11,117,200", 
"$14,391,580", "$161,210", "$182,500", "$2,145,900", "$250,000", 
"$270,500", "$298,810", "$3,452,010", "$449,001", "$6,034,000", 
"$621,710", "$7,642,660", "$700,000", "$85,100", "$885,000", 
"$923,700"), class = "factor")), .Names = c("Type", "Cost"), class = "data.frame",   row.names = c(NA, 
-19L))

here is my code in R:

p<- ggplot(data = DF, aes(x=Type, y=Cost)) + 
 geom_bar(aes(fill=Type),stat="identity") + 
 geom_line()+
scale_x_discrete (name="Type")+
scale_y_discrete(name="Cost")+
theme(axis.text.x = element_text(colour="black",size=11,face="bold")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
labs(fill=expression(paste("Type of study\n")))

print(p)

Upvotes: 0

Views: 826

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98439

Here are some starting points for your plot:

1) First, converted variable Cost from factor to numeric and named it Cost2

DF$Cost2<-as.numeric(gsub("[^0-9]", "",DF$Cost))

2) Converted your plot to grey scale using scale_fill_manual() - here all bars are grey except bar for Other that is black. With scale_y_continuous() made y axis values again as dollars with labels=dollar (for this you need to add library scales). To make Other label of x axis bold while others are normal you should provide argument face= inside theme() axis.text.x= with vector of the same length as number of levels - 1 for all levels and 2 for level Other.

library(scales)
ggplot(data = DF, aes(x=Type, y=Cost2)) + 
  geom_bar(aes(fill=Type),stat="identity",show_guide=FALSE) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1,
                                   face=(as.numeric(levels(DF$Type)=="Other") + 1)))+
  scale_y_continuous(labels=dollar)+
  scale_fill_manual(values=c("grey43","black")[as.numeric(levels(DF$Type)=="Other")+1])

enter image description here

Upvotes: 4

Related Questions