How to draw leader lines - r

The task is to draw a 3D pie chart with leading lines (leader lines in Microsoft Excel) starting from the midpoint of each sector to the associated label.
Using the below code, I am able to draw a 3D pie chart with sectors and labels and with different colors for each sector. I also wanted to include the leading lines (called leader lines in Microsoft Excel). I do not know how we draw leading lines for 3D pie charts in R. Please help me with an R script that can draw leading lines for 3D pie charts.
The initial code is as below:
library(plotrix)
# Data for Pie chart
x = c(20, 20, 20,20,20)
labels = c('breakfast', 'brunch', 'lunch', 'snacks', 'dinner')
colors = c('#4286f4','#bb3af2','#ed2f52','#efc023','#ea7441')
# Give the chart file a name.
png(file = "diet3d.png")
pie3D(x, labels=labels, explode=0.1, height=0.05, main='Daily Diet Plan', col=colors, radius = 0.5)
dev.off()

Related

In R, how can I get a contour plot overlayed on a graph without the axes being distorted?

In R, I am able to perfectly create a contour plot and a separate graph of various functions. However, when I go to plot them together, because the contour plot has a key to the right of the graph that does not exist in the other graph, the function graph axes are distorted, and the lines go off the graph.
For example, I graph the line y=10000-x, and my x-axis goes from 0 to 10000. This graphs fine on its own, but when I add it to the contour plot graph, even though the contour plot graph has the same scale, the graph of y=10000-x goes off of the graph, because the location of the x=10000 area of the graph has shifted to the right to make room for the key.
I have attached my code a photo that better illustrates the problem.
x = seq(0, 10000,length.out=100)
y = seq(0, 10000,length.out=100)
z<- outer(x,y,function(x,y) (339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y))
filled.contour(x, y, z,add=TRUE,col=hcl.colors(25, "Spectral"))
eq1 = function(x){(0*x)+8000} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,xlab="", ylab="",ylim=c(0,10000),col="purple",lwd=3)
eq2 = function(x){10000-x} #equation goes in the brackets
curve(eq2, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="blue",lwd=3)
abline(v=5000,col='green',lwd=3)
abline(v=0,col='black',lwd=3)
eq1 = function(x){(0*x)} #equation goes in the brackets
curve(eq1, from=0, to=10000,add=TRUE,ylim=c(0,10000),col="black",lwd=3)
text(2000,4000,labels="feasible region")
title(main="Constraints of TV setup",xlab="Number of 19'' sets", ylab="Number of 21'' sets")
points(3846,6154,col="red",pch=16)
text(3846,6800,labels="max",col='red')
N = function(x,y){(339-0.01*x-0.003*y)*x+(399-0.01*y-0.004*x)*y-(400000+195*x+225*y)}
cat(N(3846,6154))

Overlay ggplot pie chart on a rworldmap plot

I have created multiple pie charts with legends in ggplot. I am also using rworldmap to create maps of Eurasia with coordinates plotted as points on the map.
The pie charts I have correspond to the points I have plotted on my maps. My end goal is to overlay the pie charts I have created in ggplot on to my rworldmap plots, and then display all of the maps in a grid. I would also like to add the legends from my ggplot pie charts to the maps.
I have added my pie chart to an object(?) AD_Pie, using AD_Pie <- ggplot(df, aes...) + etc.
I have created my map using:
AD_Map <- getMap(resolution = "low")
plot(AD_Map, xlim = c(-27.0, 174.0), ylim = c(17.5, 75.0), col = "grey",border = "darkgrey", xlab = "", ylab = '' , bg= "lightblue")
points(Coordinate_AD_Clean$long, Coordinate_AD_Clean$lat, col = "red", pch = "+")
Coordinate_AD_Clean is my data frame.
My question is whether there is a way to overlay AD_Pie on to my plot in rworldmap. There doesn't need to be any interaction between the two plots, so hypothetically it would be enough to have even an image of the pie chart overlayed on to the map saved as a single plot that I can then add to a grid/matrix of all the completed plots.
There is a mapPies function in rworldmap and some functionality to add new pie charts to maps using this package, but the pies I have created in ggplot have taken significant data formatting and tweaking to get to, so using this function is not really an option.
I am very much hoping that some sort of overlaying function exists in either ggplot or rworldmap that I can use to put these pieces together. If you can help I would appreciate it greatly. Thanks for reading, and let me know if any further information is required.
Not exactly an answer, but solutions may be easier by using the ggmap package instead of rworldmap.

Adding sample size data to pie chart in R (mapplots package)

I have been able to plot several pie charts overtop a map, representing different populations. However, what I would like to do is somehow represent the sample size for each of the pie charts, as its differs between population. I have a loop to add each population present in the dataset as a pie chart:
map("worldHires", xlim=c(-140, -110), ylim=c(48, 64), col="lightgray", fill=TRUE)
points(x=-120.43,y=50.34, col="black", pch=19)
segments(x0=dataframe$Long, y0=dataframe$Lat, x1=dataframe$Long2, y1=dataframe$Lat2, col="black")
add.pie(z=c(2, 5, 6),x=-122.43,y=52.34,labels="",radius = 1)
for(i in 1:nrow(dataframe))
{
add.pie(as.integer(dataframe[i,c("Cat1","Cat2", "Cat3")]*100),
x=dataframe$Long2[i],y=dataframe$Lat2[i],labels="",radius = 0.08,
col=c("red","blue", "green"))
}
title(ylab="Latitude")
title(xlab="Longitude")
box(which="plot")
I would like to add the sample size data (dataframe$n) somehow. I've seen examples of scaled radius pie charts, which could work here, or even just adding the sample size above the pie chart. To get the sample size above the pie chart I tried adding 'main=dataframe$n' between labels and radius in the add.pie portion of the code, but this did not work. Does anyone have any ideas on how to add this to my script? Thank you.
The size of each pie is plotted according each value in your dataframe. The good dataframe for this has a stations as rows and the class type are columns

How can I make legend next to my piechart in R?

I have made a piechart in R with the next code:
#make slices
slices <- c(19, 26, 55)
# Define some colors
colors <- c("yellow2","olivedrab3","orangered3")
# Calculate the percentage for each day, rounded to one decimal place
slices_labels <- round(slices/sum(slices) * 100, 1)
# Concatenate a '%' char after each value
slices_labels <- paste(slices_labels, "%", sep="")
# Create a pie chart with defined heading and custom colors and labels
pie(slices, main="Sum", col=colors, labels=slices_labels, cex=0.8)
# Create a legend at the right
legend("topright", c("DH","UT","AM"), cex=0.7, fill=colors)
But I want the legend next to my piechart. I have also tried the following code: legend("centreright", c("DH","UT","AM"), cex=0.7, fill=colors).
But this does not give me a legend next to my pie chart.
Which code do I have to use to make a legend next to my pie chart in the middle?
You can play with the x and y argument from legend (cf ?legend):
legend(.9, .1, c("DH","UT","AM"), cex = 0.7, fill = colors)
However, a pie chart may not be the best way to represent your data, because our eye is not very good in assessing angles. The only usecase where a pie chart seems reasonable to me is when comparing 2 categories, because due to watches we can assess these proportions rather easily.

Show Up only one Piece of a Pie Chart

In plotrix I would like to make a pie chart like this:
pieval<-c(2,4,6,8)
pielabels<- c("We hate\n pies","We oppose\n pies","We don't\n care","We just love pies")
lp<-pie3D(pieval,radius=0.9,labels=pielabels,explode=0.1,main="3D PIE OPINIONS")
And I would like only the pie piece corresponding to "We just love pies" to show up, which should give something like this:
But of course I fail to make it because I use this code:
lp<-pie3D(pieval[4],radius=0.9,labels=pielabels[4],explode=0.1,main="3D PIE OPINIONS")
If you just want to draw one 3d tilted pie sector, use draw.tilted.sector.
Display a 3D pie sector
Description:
Displays a 3D pie sector.
I did experiment with setting the colour and border colour of pie segments to NA but I couldn't get rid of the shading.
But as expressed in comments, only use 3d pies if you are making a comment about how poor 3d pie charts are.
I used the draw.tilted.sector suggested by this answer.
I managed to get rid of all the shading, borderlines and sectors by making them white.
pieval <- c(2,4,6,8)
pielabels <- c("","","","We just love pies")
#make everything white
lp <-pie3D(pieval,radius=0.9,labels=pielabels,explode=0.1,
main="3D PIE OPINIONS", col= "white", shade =0, border="white")
#draw the sector
draw.tilted.sector(start = (24/20)*pi, end = 2*pi,
radius= 0.9, explode =0.1, col= "purple")
Instead you can use sector.order to display any sector according to your choice. For this case you can use
pie3D(pieval, radius=2, labels="We hate\n pies", explode=0.1, main="3D PIE OPINIONS", col=c("brown", "#ddaa00", "pink", "#dd00dd"), sector.order = 4)

Resources