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.
Related
I have a question about facet_wrap() in ggplot2.
I am trying to make a graph that looks like this. I attach an example image 1.enter image description here
In image 1 it can be seen that there are two maps and each one has its legend and color scale. I would like to be able to do this with ggplot and the facet_wrap() function.
My problem is that because the data in the dataframe is very different, they have a lot of amplitude for each map, when plotting the scale it does not allow me to visualize it the way I want.
enter image description here
ggplot(dataframe,mapping=aes(x=lon,x=lat))+
geom_contour_fill((aes(z=hgt,fill=stat(level)))+
geom_contour(aes(z=hgt),color="black",size=0.2)+
scale_fill_distiller(palette = "YlOrBr",direction = 1,super=ScaleDiscretised)+
mi_mapa+
coord_quickmap(xlim = range(dataframe$lon),ylim=range(dataframe$lat),expand = FALSE)+
facet_wrap(~nombre_nivel,scales="free", ncol =2) +
labs(x="Longitud",y="Latitud",fill="altura",title = "campos")
my dataframe has a shape like this. Where the facets are determined by the level variable. In this case the dataframe has another variable which is temp instead of hgt, but it's just another name.
enter image description here
Thanks
I think I've faced the alike problem building the two parts of the single map with two different scales. I found the package grid useful.
library(grid)
grid.newpage()
print(myggplot, vp = specifiedviewport)
In my case I built the first p <- ggplot() than adjusted p2 <- p + ...
with printing the two ggplots (p and p2) in two viewports. You can newly construct p2 with individual scale and print it in the grid. You can find useful information
here.
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()
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
I am trying to do multipanel plots one panel being a heatmap using layout to place plots. I've been drawing heatmaps with pheatmap which provides a very convenient color scheme among other things.
The code for pheatmap is available here.
When I try using pheatmap in this way it always plots on a new page. I imagine this is because of its use of the grid package? Is there a way I can do this with pheatmap?
Example code to produce a heatmap next to a barplot but which doesn't since the heatmap gets plotted on a new page below:
xlay=layout( matrix(c(2,2,1),nrow=1) )
layout.show(xlay)
barplot(rnorm(8),horiz=T)
pheatmap(matrix(rnorm(80),nrow=8))
Make your bar plot in ggplot
bar <- ggplot()
Assign both the barplots and heatmap to a variable
heat <- pheatmap(matrix(rnorm(80),nrow=8))
then use gridExtra package to make panel plot the heatmap is saved as an object and you can plot it again by assessing the 4th item in the object
grid.arrange(bar, heat[[4]], nrow = 1)
I want to create a contour plot that has a color key next to it.
I would make the contour plot like this:
data(volcano)
image(volcano)
contour(volcano, add = TRUE)
However, image() does not support a legend as far as I know. I saw that there is also a contourplot() function in the lattice package, however, I cannot figure it out how to overlay
the map with the contour lines when using levelplot(volcano) and contourplot(volcano)
Ideally I want my plot to look like this but with a color key:
You could use image.plot from the fields package. This creates a legend by default.
library(fields)
image.plot(volcano)
contour(volcano, add = TRUE)