I am writing a document in R markdown using the knitr package, which has to be provided as HTML for online viewing and as pdf for offline reading. There are some inconsistencies in the output of the plots as shown in html and in the pdf document.
- bar plot legend is not properly display in pdf
- pie diagram legend overlapping with the plot in pdf
items <- c('Food', 'Clothing', 'House Rent', 'Education',
'Litigation', 'Conventional Needs', 'Miscellaneous')
familyA <-c(24,4,4,3,2,1,2)
familyB <-c(60,14,16,6,10,6,8)
df = data.frame(items, familyA, familyB)
familyAPerc <- prop.table(familyA) * 100
familyBPerc <- prop.table(familyB) * 100
df <- cbind.data.frame(items, familyA, familyAPerc, familyB, familyBPerc)
subdf <- df[, c(3,5)]
par(xpd=T, mar=c(5,4,1.4,0.2))
barplot(as.matrix(subdf), width=c(0.4, 1.2), legend.text = df$items, cex.main=0.6,
args.legend = list(x="bottom", ncol=4, cex=0.6, bty='n', inset=-0.2))
Changing inset to -0.3 fix the output in the pdf but the legend is cropped in html.
x <- c(50, 30, 20,15,35)
labels <- c("food","clothing","house rent","fuel & light", "miscellaneous")
piepercent<- round(100*x/sum(x), 1)
pie(x, labels = piepercent, main = "Pie Diagram",col = rainbow(length(x)))
legend("topright", labels, cex = 0.8, fill = rainbow(length(x)))
The entire R project is available on this github repo
How can I get proper plot display in both html and pdf outputs without spending hours? Please help.
/fig/tablesketch.png is not available error
Maybe you didn't upload that .PNG into the repo? Even the pioneer of Rmarkdown, Yihui Xie said sometimes the best resolution is to render it to HTML, and then PRINT the HTML webpage to the PDF from the web browser. – Daniel Jachetta