0

I have the following vector:

percentage <-c(-97, -97, -38, -4, 12)

I then use this function to format the numbers and convert them to text:

percent_fun <- function(x, digits = 0, format = "f", ...) {
  paste0(formatC(x, format = format, digits = digits,...), "%")
}
percentage<-percent_fun(percentage)

The intention is to further use this in a chart. My question is how I can specify in the above function to use 'Times New Roman' and make the size of the fonts/percentages equal to 8.

Here is what I plot:

structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01, 
0.11, 5.83, 21.4, 72.64), quantile = structure(c(4L, 1L, 2L, 
3L, 5L, 4L, 1L, 2L, 3L, 5L), .Label = c("2nd Quantile", "3rd Quantile", 
"4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"), 
    case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, 
-10L), class = "data.frame")



 p<-ggplot(df_cum, aes(fill = case , quantile, shares)) + geom_bar(position =
                                                                    "dodge", stat = "identity") +
  scale_x_discrete(limits = c(
    "Poorest 20%",
    "2nd Quintile",
    "3rd Quintile",
    "4nd Quintile",
    "Richest 20%"
  )) +
  scale_fill_manual("",values = rev(RColorBrewer::brewer.pal(3,'Blues')), labels = c("Before Public Debt Shock", "Aefore Public Debt Shock")) +
  xlab("Quintile") +
  ylab("Wealth Shares (%)") +
  theme_minimal()+
  theme(text =element_text(family="Times New Roman"),
        axis.title=element_text(size=8),
        axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)),
        plot.caption=element_text(size = 8, hjust=0),
        #legend.position="bottom", legend.box = "horizontal",
        legend.position=c(0.2,0.9), legend.box = "horizontal",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.text = element_text(size = 8),
        legend.title.align = 0,
        legend.title = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x = element_blank()) +
  guides(colour=guide_legend(nrow=2))

print(p)
y = rep(c(3, 5, 13, 25, 75),2)
x = rep(c(1:5), 2)
label = rep(percentage, 2) 
p1 <- p + geom_text(x=x, y=y+2, label=label)
p1 + geom_errorbarh(aes(xmax = (x + 0.3), xmin = (x - 0.3), y = y), height = 0.5) 
print(p1)
msh855
  • 1,493
  • 1
  • 15
  • 36
  • You have to provide some code for plotting. The question is impossible to answer without knowing what packages you use etc. If you want to use `ggplot2` have a look here https://stackoverflow.com/questions/27433798/how-can-i-change-the-y-axis-figures-into-percentages-in-a-barplot – JBGruber Sep 15 '19 at 10:17
  • Please see my updated answer – msh855 Sep 15 '19 at 10:20
  • I think you are seeking for `extrafont` library. Have a look at [this](https://stackoverflow.com/questions/27689222/changing-fonts-for-graphs-in-r?rq=1) one please. – maydin Sep 15 '19 at 10:24
  • I have this and it works, except for the 'percentage' vector .. will update my answer. – msh855 Sep 15 '19 at 10:27

1 Answers1

0

I found myself a solution. The only modification is to use more options on the geom_text(). Here is my answer:

  y = rep(c(3, 5, 11, 23, 65),2)
  x = rep(c(1:5), 2)
  label = rep(percentage, 2) 
p<-ggplot(df_cum, aes(fill = case , quantile, shares)) + 
  geom_bar(position ="dodge", stat = "identity") +
  **geom_text(x=x, y=y+2, label=label, size = 3.5, family = "Times New Roman")** + 
  geom_errorbarh(aes(xmax = (x + 0.3), xmin = (x - 0.3), y = y), height = 0.5) +
  scale_x_discrete(limits = c(
    "Poorest 20%",
    "2nd Quintile",
    "3rd Quintile",
    "4nd Quintile",
    "Richest 20%"
  )) +
  scale_fill_manual("",values = rev(RColorBrewer::brewer.pal(3,'Blues')), labels = c("Before Public Debt Shock", "Aefore Public Debt Shock")) +
  xlab("Quintile") +
  ylab("Wealth Shares (%)") +
  theme_minimal()+
  theme(text =element_text(family="Times New Roman"),
        axis.title=element_text(size=8),
        axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)),
        plot.caption=element_text(size = 8, hjust=0),
        #legend.position="bottom", legend.box = "horizontal",
        legend.position=c(0.3,0.9), legend.box = "horizontal",
        legend.background = element_blank(),
        legend.key = element_blank(),
        legend.key.size = unit(0.3, "cm"),
        legend.text = element_text(size = 8),
        legend.title.align = 0,
        legend.title = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x = element_blank()) +
  guides(colour=guide_legend(nrow=2))

print(p)
msh855
  • 1,493
  • 1
  • 15
  • 36