1

I'm trying to create a stacked barplot with the distribution of genotype at different ages. I have come quite far, but now I don't know how to solve my problem: There is a space in my x-axis between two plots. How can I remove this blank. I guess it is because my X-values are not continous, but I don't know how to change the script. Furthermore, if anyone can help me to add the number of observation (30 for each age) I would be really grateful.

I tried to put the age vector into characters, but that only led that the X-Axis was completly rearranged

This is my complete script:

Age<-c(4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,10,10,10,10,11,11,11,11,12,12,12,12)
Genotype<-c("UN", "HM", "HT", "WT","UN", "HM", "HT", "WT","UN", "HM", "HT", "WT","UN", "HM", "HT", "WT","UN","HM", "HT", "WT","UN","HM", "HT", "WT","UN","HM", "HT", "WT")
Percentage<-c(6.7, 26.7, 23.3, 43.3, 10, 13.3, 26.6, 50, 0, 36.6, 23.3, 40, 13.4, 40, 33.3, 13.3, 6.8, 26.6, 40, 26.6, 10, 23.3, 53.3, 13.3, 13.4, 20, 63.3, 3.4)

Plastics_sec<-data.frame(Age, Genotype, Percentage)

Plastics_sec$Genotype<- factor(Plastics_sec$Genotype, levels = c("UN","HM","HT", "WT"), labels = c("Undefined", "Homozygous","Heterozygous", "Wildtype"))

fill<-c("khaki1", "brown1", "darkolivegreen3", "deepskyblue3")

plot1<- ggplot() + geom_bar(aes(y = Percentage, x = Age, fill = Genotype), data = Plastics_sec, stat="identity")+ theme(legend.position="right", legend.direction="vertical", legend.title = element_blank()) + labs(x="Age", y="Percentage") + scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) + ggtitle("BBS4 Genotype at different ages")+scale_fill_manual(values = fill)

plot1

I would like, taht there is no space between Age 7 and 10. And furthermore I would like to add the number of observation (30 for each age) on top of each bar (I know there is already some scripted posted, but I didn't manage to apply it to my script).

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 1
    You can simply solve this problem by using factor (`x = factor(Age)`). With it you're turning continuous variable to a categorical. Let me know if this works. – pogibas Jun 22 '19 at 08:04
  • well i don't have the space anymore, but know the x-axis is rearranged with 10-11-12-4-5-6-7 – Joanne Vialle Jun 22 '19 at 08:06
  • The x-axis is in the correct order for me when using `factor(Age)`. I would try with a new session of R, using your code above and applying that change, and see if it's still unordered. – heds1 Jun 22 '19 at 08:08
  • yes, this might happen because data is not continuous anymore and R doesn't know it's order. You should try adding this `scale_x_discrete(limits = factor(sort(unique(Age))))`. It tells R what order x axis should be (here you're taking sorters unique values of `Age`). Does it work? – pogibas Jun 22 '19 at 08:12
  • Yeah reopening R helped! Thanks a lot! One last question, I open you help me with. How can I add, the number of observation? Do you have any idea? Thanks again for the help! – Joanne Vialle Jun 22 '19 at 08:14
  • Please explain what you mean by *How can I add, the number of observation* – pogibas Jun 22 '19 at 08:18
  • Well For each age I observed 30 individuals. And basically I like that on top of each bar there is written N=30. I just don't know how to incoroprate the 30 in my data.frame – Joanne Vialle Jun 22 '19 at 08:21
  • IMO if number is the same for all the observations then it would clutter the plot. How about you add this information in a title/subtitle (which is more common practice). For example - `+ labs(subtitle = "Number of samples per age group = 30")` – pogibas Jun 22 '19 at 08:25
  • Yeah, that's actual a good idea! Thanks a lot for your help! – Joanne Vialle Jun 22 '19 at 08:29
  • Yeah that way is probably the best, but just for interest, this should do what you initially wanted: `+ geom_text(aes(x=factor(Age),y=100,label="N = 30",vjust=-0.5))` – heds1 Jun 22 '19 at 08:30
  • Happy to help! And welcome to Stackoverflow :-) I'll be closing your question now as it's a duplicate to previous questions that were asked here before. – pogibas Jun 22 '19 at 08:31

0 Answers0