E B E B - 2 months ago 6
R Question

in R ggplot how to change labels for multiple barplots

I am trying to change the label of a multi-variable plot in ggplot
My first image is this:
enter image description here

The first image shows that I have two variables - Count and Total Gross
And I have x labels as G and N (which are the variable values I have in the data)
I want to change the x-labels to be more descriptive

how can I update my ggplot statement to introduce these new labels

test %>%
group_by(DiscInd) %>%
summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>%
gather(Var, Val, -DiscInd) %>%
ggplot(., aes(x=DiscInd, y = Val, fill=Var)) +
geom_bar(stat="identity", position="dodge") +
xlab("Year vs Released Difference") +
ylab("Total Gross") +
ggtitle("Total Movie with Gross ")


Is this possible?

Here are some records for my dataframe test

DiscInd Gross
N 2783918982
N 2207615668
N 1670328025
N 1519479547
G 1514019071
G 1404705868

Answer

Let's say your new descriptive label names are Label G Desc and Label N Desc , is this what you want?

test %>%
  group_by(DiscInd) %>% 
  summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>% 
  gather(Var, Val, -DiscInd) %>% 
  ggplot(., aes(x=DiscInd, y = Val, fill=Var)) +
  geom_bar(stat="identity", position="dodge") +
  scale_x_discrete(labels=c("Label G Desc", "Label N Desc")) +
  xlab("Year vs Released Difference") + 
  ylab("Total Gross")                 +
  ggtitle("Total Movie with Gross ")  

x-axis text with angle:

enter image description here

test %>%
  group_by(DiscInd) %>% 
  summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>% 
  gather(Var, Val, -DiscInd) %>% 
  ggplot(., aes(x=DiscInd, y = Val, fill=Var)) +
  geom_bar(stat="identity", position="dodge") +
  scale_x_discrete(labels=c("Label G Desc", "Label N Desc")) +
  xlab("Year vs Released Difference") + 
  ylab("Total Gross")                 +
  ggtitle("Total Movie with Gross ")  +
  theme(axis.text.x = element_text(angle = 60, size=15, hjust = 1))

enter image description here

Comments