原始碼 InfoField |
R code
|
The data can be edited at [[User:T.seppelt/UK EU referendum polling.csv]]. [[:User:T.seppelt|T.seppelt]] is going to refresh the diagram after updating this page. The diagram was generated with this script:
require(ggplot2)
library(scales)
download.file("https://commons.wikimedia.org/w/index.php?title=User:T.seppelt/UK_EU_referendum_polling.csv&action=raw", destfile = "/tmp/test.csv",method="curl")
data <- read.csv("/tmp/test.csv", dec=".", sep="," ,colClasses=c("Date","numeric","numeric","numeric","numeric",NA, NA));
data$remain <- data$remain / 100
data$leave <- data$leave / 100
data$undecided <- data$undecided / 100
df1 <- data.frame(date = data$date, remain = data$remain,leave = data$leave,undecided = data$undecided);
green <- "#24B14C"
svg("UK EU referendum polling.svg", width = 10, height = 7, family="sans-serif", bg="transparent")
g <- ggplot(df1,aes(x=date));
g <- g + scale_x_date(labels = date_format("%Y-%m-%d"), minor_breaks="2 week");
g <- g + labs(title = "United Kingdom European Union membership referendum \n Opinion polling")
g <- g + xlab("date");
g <- g + ylab("proportion of the interviewed persons")
g <- g + geom_point(data=df1,aes(x=date,y=remain,colour="Remain"));
g <- g + geom_smooth(data=df1,aes(x=date,y=remain), colour=green)
g <- g + geom_point(data=df1,aes(x=date,y=leave,colour="Leave"));
g <- g + geom_smooth(data=df1,aes(x=date,y=leave), colour="red")
g <- g + geom_point(data=df1,aes(x=date,y=undecided,colour="Undecided"));
g <- g + geom_smooth(data=df1,aes(x=date,y=undecided), colour="blue")
#g <- g + geom_line(aes(y=50),color="black")
g <- g + scale_colour_manual("",
breaks = c("Remain", "Leave", "Undecided"),
values = c("red", green, "blue"))
g <- g + scale_y_continuous(labels=percent)
g <- g + theme_classic() + theme(
rect = element_rect(fill = "transparent",colour = NA),
panel.grid.major = element_line(colour = "#bbbbbb", size = 0.5),
panel.grid.minor = element_line(colour = "#cccccc", size = 0.3),
panel.background = element_rect(fill = "transparent",colour = NA),
legend.key = element_rect(fill = "transparent",colour = NA)
) + theme(legend.position="top")+theme(plot.title=element_text(size=20));
g
dev.off();
g
|
R code (2)
|
I had to make some slight adjustments to the script to get it to work with R 3.2.3 and ggplot2 2.0.0. – [[:User:Kanoch|Kanoch]]
require(ggplot2)
library(scales)
download.file("https://commons.wikimedia.org/w/index.php?title=User:T.seppelt/UK_EU_referendum_polling.csv&action=raw", destfile = "/tmp/test.csv",method="curl")
data <- read.csv("/tmp/test.csv", dec=".", sep="," ,colClasses=c("Date",NA,NA,NA,NA,NA,NA));
data$remain <- data$remain / 100
data$leave <- data$leave / 100
data$undecided <- data$undecided / 100
svg("UK EU referendum polling.svg", width = 10, height = 7, family="sans-serif", bg="transparent")
df1 <- data.frame(date = data$date, remain = data$remain,leave = data$leave,undecided = data$undecided);
g <- ggplot(df1,aes(x=date,df1));
g <- g + scale_x_date(date_labels="%Y-%m-%d", date_minor_breaks="2 week");
g <- g + labs(title = "United Kingdom European Union membership referendum \n Opinion polling")
g <- g + xlab("date");
g <- g + ylab("proportion of the interviewed persons")
g <- g + geom_point(data=df1,aes(x=date,y=remain,colour="Remain"));
g <- g + geom_smooth(data=df1,aes(x=date,y=remain), colour="green")
g <- g + geom_point(data=df1,aes(x=date,y=leave,colour="Leave"));
g <- g + geom_smooth(data=df1,aes(x=date,y=leave), colour="red")
g <- g + geom_point(data=df1,aes(x=date,y=undecided,colour="Undecided"));
g <- g + geom_smooth(data=df1,aes(x=date,y=undecided), colour="blue")
g <- g + scale_colour_manual("",
breaks = c("Remain", "Leave", "Undecided"),
values = c("red", "green", "blue"))
g <- g + theme(legend.position="top")+theme(plot.title=element_text(size=20));
g <- g + scale_y_continuous(labels=percent)
g
dev.off();
|
|