0

I am trying to using ggplot2. The problem I faced there is labels of bars are up and down for each two bars of the graph. I would like to be each label on its own bar. I would like you to help me in fix this problem.

Also, I would like to know if there is a possibility to add "k$" to the label to each bar. I would like to know if there is a possibility to put the indicators (Actual and Budget) on the top of the graph.

This is my code:

library (ggplot2)

library(plotly)

library(readxl)

library(tidyverse)

df =read_excel("C:/Fall 2020 - Clarkson University/Information Visualization/Assignments/Assignment2/a2-DeptYearlyPerf.xls")

df2= df %>% pivot_longer(-Dept.,names_to="category", values_to="amount")

df3=df2 %>% separate(category,into=c("qtr","bud_or_act"),sep=" ")

df3 %>% ggplot(aes(x=qtr,y= amount,fill=bud_or_act, label= amount, width=.7)) +
  
  geom_col(position="dodge") +facet_wrap(~Dept.) +
  
  geom_text(size=3, position = position_stack(vjust = 0.5)) 

enter image description here

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66
Elwakdy
  • 41
  • 1
  • 1
  • 6

1 Answers1

0

I don't have your data hence I can not help you with the code however you may find the ggrepel package helpful.

Or you can try:

df2= Book1 %>% pivot_longer(-Dept,names_to="category", values_to="amount")

df3=df2 %>% separate(category,into=c("qtr","bud_or_act"),sep=" ")

df3 %>% ggplot(aes(x=qtr,y= amount,fill=qtr, label= amount, width=.7)) +
  
geom_bar(stat='identity',position="dodge") +facet_wrap(~Dept) +
  
geom_text(size=3,hjust = 0.5)

Result on my computer with hypothetical numbers: https://ibb.co/DrSsXCG

EfehanD
  • 26
  • 4
  • The data and the graph have been attached to my question – Elwakdy Sep 15 '20 at 17:32
  • Just made changes since I could not see the plots initially. – EfehanD Sep 15 '20 at 17:41
  • The labels were up and down for each two bars of the graph. I would like to be each label on its own bar. I can update my question and send you the graph if you wish. – Elwakdy Sep 15 '20 at 17:42
  • Can you try to align with hjust parameter? – EfehanD Sep 15 '20 at 17:46
  • I update the question so you can see the graph I got – Elwakdy Sep 15 '20 at 17:51
  • I tried to use "hjust" parameter and I got this error. Error in position_stack(hjust = 0.5) : unused argument (hjust = 0.5) – Elwakdy Sep 15 '20 at 17:52
  • Can you try: geom_text(size=3, position=position_dodge(), hjust=-0.5) – EfehanD Sep 15 '20 at 17:54
  • I tried to do that, but I got this Warning message: Width not defined. Set with `position_dodge(width = ?)` – Elwakdy Sep 15 '20 at 17:56
  • This should help you :https://stackoverflow.com/questions/34889766/what-is-the-width-argument-in-position-dodge/35102486. by convering it to position = "dodge" – EfehanD Sep 15 '20 at 17:58
  • There is overlapping between the labels – Elwakdy Sep 15 '20 at 17:59
  • I tried this df3 %>% ggplot(aes(x=qtr,y= amount,fill=bud_or_act, label= amount, width=.7)) + geom_col(position="dodge") +facet_wrap(~Dept.) + geom_text(size=3, position_dodge(width = NULL), hjust=-0.5) – Elwakdy Sep 15 '20 at 18:03
  • but I am still having overlapping between labels – Elwakdy Sep 15 '20 at 18:03
  • Still overlapping with position = "dodge", hjust=-0.5? – EfehanD Sep 15 '20 at 18:05
  • Yes, when I write those line code:df3 %>% ggplot(aes(x=qtr,y= amount,fill=bud_or_act, label= amount, width=.7)) + geom_col(position="dodge") +facet_wrap(~Dept.) + geom_text(size=3, position = "dodge", hjust=-0.5) – Elwakdy Sep 15 '20 at 18:11
  • I got this Warning message: Width not defined. Set with `position_dodge(width = ?)` – Elwakdy Sep 15 '20 at 18:12
  • Just reproduced on my laptop with some hypothetical numbers and worked fine with me. `df2= Book1 %>% pivot_longer(-Dept,names_to="category", values_to="amount") df3=df2 %>% separate(category,into=c("qtr","bud_or_act"),sep=" ") df3 %>% ggplot(aes(x=qtr,y= amount,fill=qtr, label= amount, width=.7)) + geom_bar(stat='identity',position="dodge") +facet_wrap(~Dept) + geom_text(size=3,hjust = 0.5)` – EfehanD Sep 15 '20 at 18:54