data.frame(
x1 = factor(sample(c(1L,2L,3L),100,replace=T)),
x2 = runif(100),
y = runif(100),
z1 = rnorm(100),
z2 = factor(sample(letters[1:4],100,replace=T))
) -> df_example
df_example %>%
ggplot()+
geom_boxplot(
aes(x=x1,y=y,fill=z2)
) -> basicBoxplot
basicBoxplot
colorspace::qualitative_hcl(n = 7, h = c(0, 360), c = 35, l = 85, register = "Custom-Palette")
basicBoxplot +
scale_fill_discrete_qualitative(palette="Custom-Palette",nmax=5)
library(readr)
disposableIncome <- read_csv("https://www.dropbox.com/s/z80sbjw94cjex8x/disposableIncome.csv?dl=1",
locale = locale(encoding = "BIG5"), skip = 4)
library(dplyr)
library(tidyr)
colnames(disposableIncome)[[1]] <- "年份"
disposableIncome %>%
.[-c(44:49),] -> disposableIncome
as.numeric(disposableIncome$年份) ->
disposableIncome$年份
disposableIncome %>%
filter(年份 >= 2003) %>%
gather(
-1,
key = "組別", value = "元"
) %>%
ggplot() +
geom_line(
aes(x=年份, y=元, color = 組別)
)
`Encoding<-`(disposableIncome$X1,"utf-8") -> disposableIncome$X1
load(url("https://github.com/tpemartin/course-108-1-inclass-datavisualization/blob/master/%E4%BD%9C%E5%93%81%E5%B1%95%E7%A4%BA/graphData_homework2019-10-08_014.Rda?raw=true"))
# rename the column names cause its messy and just wrong
colnames(graphData$travelerFromAsia) <- c("年分", "地區", "來台旅遊人數(萬)")
graphData$travelerFromAsia$年分 <- as.numeric(graphData$travelerFromAsia$年分)
graphData$travelerFromAsia %>%
ggplot(aes(x=`年分`,y=`來台旅遊人數(萬)`,color = 地區, linetype = 地區)) +
geom_line()
graphData$traffic %>%
melt(id.vars="年別") %>%
ggplot(aes(x=年別,
y=value)) +
geom_line(aes(color=variable),size=1.2
) +
geom_vline(aes(xintercept=2015),size = 0.005)+
geom_hline(aes(yintercept=0),linetype="twodash",size = 0.05,color="red") +
scale_color_discrete_sequential(palette="Custom-Palette")+
labs(title = "2012-2017大眾運輸工具載客變化",
subtitle="(以2012為基期)",
x = "年份",y = "載客量",legend.title = element_text("運輸工具"))
graphData$traffic %>%
melt(id.vars="年別") %>%
ggplot(aes(x=年別,
y=value)) +
geom_line(aes(color=variable),size=1.2
) +
geom_vline(aes(xintercept=2015),size = 0.005)+
geom_hline(aes(yintercept=0),linetype="twodash",size = 0.05,color="red") +
scale_color_discrete_sequential(palette="Custom-Palette")+
labs(title = "2012-2017大眾運輸工具載客變化",
subtitle="(以2012為基期)",
x = "年份",y = "載客量")+
scale_fill_discrete(name = "運輸工具")
scale_color_discrete_sequential(...., guide=guide_legend(***))
*的設定可以看https://ggplot2.tidyverse.org/reference/guide_legend.html
getAnywhere(theme_economist)
View(ggtheme::theme_economist)
sf_mrtStops_tpe %>%
filter(
str_detect(經過路線,"BL")
) -> sf_mrtStops_tpe_BL
sf_mrtStops_BL %>% View
sf_mrtStops_BL %>%
mutate(
站號=str_extract(經過路線,"(?<=(BL))[:digit:]+")
) %>%
arrange(站號) -> sf_mrtStops_BL
sf_mrtStops_BL %>% View
sf_northTaiwan %>%
ggplot()+geom_sf()+
geom_sf(
data=sf_mrtStops_BL
)+
coord_sf(
xlim=c(121.4,121.7),
ylim=c(24.9,25.1)
) -> gg_tpe_mrt
gg_tpe_mrt