亲爱的,
我希望在一个投影仪演示中将两个连续的幻灯片放入2个等值线。边界相同但填充颜色不同,因此当我从第一张幻灯片转到第二张幻灯片时,地图不应移动,而只能翻转填充颜色。
我的问题是我使用ggplot的SAME咒语(填充颜色除外)2次来制作2个图形,但是2个图形没有对齐,所以当我从第一张幻灯片转换到第二张时,我可以看到第二个图表沿水平方向稍微移动。
由于数据的保密性质,我无法粘贴我的示例。
#这个例子来自: #http://demos.jquerymobile.com/1.4.2/panel/
library(rgdal)
library(ggplot2)
library(dplyr)
library(broom)
# clone NepalMaps from https://github.com/anjesh/NepalMaps
# read shapefile
nepal.adm3.shp <- readOGR(dsn="./NepalMaps-master/baselayers/NPL_adm", layer="NPL_adm3", stringsAsFactors = FALSE)
# tidy shapefile data to data frame
nepal.adm3.shp.df <- tidy(nepal.adm3.shp, region = "NAME_3")
#write districts to csv file
mydata <- nepal.adm3.shp.df %>%
distinct(id) %>%
write.csv("districts.csv", row.names = FALSE)
mydata <- read.csv("districts.csv")
# Make fake data
mydata$Var1 = as.factor(sample(c(1:5,NA),nrow(mydata),replace= TRUE))
mydata$Var2 = as.factor(sample(c(1:5,NA),nrow(mydata),replace= TRUE))
mydata$Var3 = as.factor(sample(c(1:5,NA),nrow(mydata),replace= TRUE))
myshapeAndData <- left_join(nepal.adm3.shp.df,mydata,by="id")
library(scales)
library(ggplot2)
library(RColorBrewer)
# Plot using ggplot2.
# EXACT same Graphs,EXCEPT,with different lengths of title / legend title (padded with spaces to make them of equal length)
# The graphs move on transitioning from one slide to the other. I have padded the plot title / legend title with spaces to make them have the same length but it does NOT work.
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var1),color= "black") +
scale_fill_manual(name ="Var1 ",values = brewer.pal(5,"Greens"),na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("Title Length 1 ") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph1.pdf",height =38/4,width = 56/4,units="in")
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var2),color = "black") +
scale_fill_manual(name = "My var 2 ",values = brewer.pal(5,"Greens"),na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("My title Length 2 ") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph2.pdf",height = 38/4,width = 56/4,units="in")
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var3),color = "black") +
scale_fill_manual(name = "3rd Variable",values = brewer.pal(5,"Greens") ,na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("My title-testing three") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph3.pdf",height = 38/4,width = 56/4,units="in")
# EXACT same graphs.
# All graphs with the same length of plot title and legend title.
# This works. The graphs do not move on transitioning,they only flip colors.
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var1),color= "black") +
scale_fill_manual(name ="Var1",values = brewer.pal(5,"Greens"),na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("Title1") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph1.pdf",height =38/4,width = 56/4,units="in")
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var2),color = "black") +
scale_fill_manual(name = "Var2",values = brewer.pal(5,"Greens"),na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("Title2") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph2.pdf",height = 38/4,width = 56/4,units="in")
ggplot() +
geom_polygon(data = myshapeAndData, aes(x=long,y=lat,group = group,fill = Var3),color = "black") +
scale_fill_manual(name = "Var3",values = brewer.pal(5,"Greens") ,na.value = "pink") +
theme(plot.margin = unit(c(.4,.8,.3,.2), "in")) +
xlab("") +
ylab("") +
theme(legend.key.size = unit(1, "cm")) +
theme(legend.text=element_text(size=15),axis.text=element_text(size=15),plot.title = element_text(size=20),legend.title =element_text(size=15),axis.title = element_text(size=15)) +
ggtitle("Title3") +
theme(plot.title = element_text(hjust = 0.5)) +
coord_fixed(ratio = 5.6/3.8)
ggsave("graph3.pdf",height = 38/4,width = 56/4,units="in")
\documentclass{beamer}
\usetheme{CambridgeUS}
\usepackage{amssymb,amsmath}
\usepackage{listings}
\usepackage{hyperref}
\usepackage{fancyvrb}
\title[]{Testing maps}
\author{}
\begin{document}
\maketitle
\begin{frame}
\centering
\begin{tabular}{c}
\includegraphics[width=0.9\linewidth]{graph1.pdf}
\end{tabular}
\end{frame}
\begin{frame}
\centering
\begin{tabular}{c}
\includegraphics[width=0.9\linewidth]{graph2.pdf}
\end{tabular}
\end{frame}
\begin{frame}
\centering
\begin{tabular}{c}
\includegraphics[width=0.9\linewidth]{graph3.pdf}
\end{tabular}
\end{frame}
\end{document}