0

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)
Community
  • 1
  • 1
SimpleNEasy
  • 879
  • 3
  • 11
  • 32
  • For the BW printing issue, just use `scale_fill_grey()` as in [this answer](http://stackoverflow.com/a/13507514/903061). – Gregor Thomas Nov 12 '13 at 17:14
  • 3
    There are some problems with your data/plot: (1) it is barplot, not the boxplot, (2) there is no need to use colors if you have just one variable for each type, (3) MAIN PROBLEM - your Cost values are factor and not numeric so your plot is totally wrong - the highest cost is almost at bottom but the lowest cost almost at top. – Didzis Elferts Nov 12 '13 at 17:18
  • shujaa Thanks I will try it. @Didzis Elferts Thanks for the note ..I changed it to barplot. 3) I used the currency which basically can be removed. I asked the question to get new idea and design !!!!!! – SimpleNEasy Nov 12 '13 at 17:41
  • 2
    @SimpleNEasy, unless you change cost to numeric, that is point less, For focusing one and making color to stand out, you can use `scale_fill_manual()` – Ananta Nov 12 '13 at 17:46
  • I changed the cost to numeric and that made R crashed many times !!!!!. – SimpleNEasy Nov 12 '13 at 18:10
  • hmm, how did you change it? it works fine for me, oh yeah, now you have to remove `scale_y_discrete(name="Cost")+` as it is not discrete – Ananta Nov 12 '13 at 18:47

1 Answers1

4

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

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201