by

## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
shuyulin97
@shuyulin97
##計算collegeData中不同縣市名稱及體系別的一年級男生及一年級女生總數（使用sum）和學校數目。
#分群不同縣市名稱及體系別
collegeData_group <- group_by(collegeData,
縣市名稱,
體系別)

#計算
summarise(collegeData_group,
一年級男生總數 = sum(一年級男生),
一年級女生總數 = sum(一年級女生),
學校數目 = n())
ningtraveo75
@ningtraveo75
group_by(collegeData,縣市名稱,體系別) -> collegeData_by
summarise(collegeData_by,
一年級男生總數=sum(一年級男生),
一年級女生總數=sum(一年級女生),
學校數目=n())
Martin老師
@tpemartin
計算每位學生每學年每學期的平均成績。
transcriptDataFinal %>%
group_by(學號,學年,學期) %>%
summarise(
平均成績=sum(學期成績*學分數)/sum(學分數)
)
KaEDe1106
@KaEDe1106
gather(df_taoyuanMarriage,
-月份區域別,
key="月份",value="")
Chen-Ying-Chen
@Chen-Ying-Chen
gather(df_taoyuanMarriage,
ends_with("月"),
key="月份",value="對數"
)
shihhanChiu
@shihhanChiu
df_taoyuanMarriage%>%
gather(
ends_with("月"),
key = "月份",value = "對數"
)
st85611
@st85611
df_taoyuanMarriage
gather(df_taoyuanMarriage,
-月份區域別,
key="月份",value="人數")
asus50901
@asus50901
df_taoyuanMarriage %>%
gather(ends_with("月"), key = "月數", value = "對數") %>%
group_by(月數) %>%
dplyr::summarise(總結婚對數 = sum(對數))
KaEDe1106
@KaEDe1106
df_taoyuanMarriage %>%
gather(-月份區域別,
key="月份",value="對數") %>%
group_by(月份) %>%
summarise(總結婚對數=sum(對數))
asus50901
@asus50901
df_taoyuanMarriage %>%
gather(ends_with("月"), key = "月數", value = "對數") %>%
group_by(月份區域別) %>%
dplyr::summarise(最高峰月份 = which.max(對數))
Chen-Ying-Chen
@Chen-Ying-Chen
df_taoyuanMarriage%>%
gather(
ends_with("月"),
key="月份",value="對數"
)%>%group_by(月份區域別)%>%summarise(結婚最高峰月份=which.max(對數))
emilyluckey
@emilyluckey
df_taoyuanMarriage %>%
gather(
grep("月$",colnames(.)), key = "年份", value = "對數" ) tim8537099 @tim8537099 library(readr) X27b5594a385573df182ebb70880cdcbe <- read_csv("http://opendataap2.hl.gov.tw/./resource/files/2019-03-15/27b5594a385573df182ebb70880cdcbe.csv") View(X27b5594a385573df182ebb70880cdcbe) funkjerry @funkjerry library(readr) DownloadFile <- read_csv("http://data.moi.gov.tw/MoiOD/System/DownloadFile.aspx?DATA=9796A044-5B71-4194-978C-5148AD7D8DB9", locale = locale(encoding = "BIG5")) View(DownloadFile) Jimmyflash229 @Jimmyflash229  library(readr) X69786f76_5d6b_4c19_8614_95c3e18ea873 <- read_csv("http://datacenter.taichung.gov.tw/swagger/OpenData/69786f76-5d6b-4c19-8614-95c3e18ea873") View(X69786f76_5d6b_4c19_8614_95c3e18ea873) emilyluckey @emilyluckey download_id_64a896aa_a6aa_4362_90fb_4270d90ff625_rid_85e11adb_c228_4b57_a7fd_1d99fb31129c <- read_csv("https://data.tycg.gov.tw/opendata/datalist/datasetMeta/download?id=64a896aa-a6aa-4362-90fb-4270d90ff625&rid=85e11adb-c228-4b57-a7fd-1d99fb31129c") KaEDe1106 @KaEDe1106 TrainDeathNumber2018 <- read_csv("http://ods.railway.gov.tw/tra-ods-web/ods/download/dataResource/8ae4cabe6c8578b2016c8a2a14043137", locale = locale()) TrainDeathNumber2018 emilyluckey @emilyluckey subsetDataTWbank %>% gather( 定存利率-一個月-固定,定存利率-二年期-固定,定存利率-三年期-固定, key = "期數", value = "利率" ) %>% ggplot() + geom_point( aes(x=期數,y=利率,color=西元年月) ) godgodgod11101 @godgodgod11101  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) emilyluckey @emilyluckey 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 = 組別) ) Martin老師 @tpemartin c('X1','平均每戶可支配所得','可支配所得按戶數五等分位組-最低所得組','可支配所得按戶數五等分位組-次低所得組','可支配所得按戶數五等分位組-中間所得組','可支配所得按戶數五等分位組-次高所得組','可支配所得按戶數五等分位組-最高所得組') -> names(disposableIncome) emilyluckey @emilyluckey Encoding<-(disposableIncome$X1,"utf-8") -> disposableIncome$X1 Encoding<-(disposableIncome$X1,"utf-8") -> disposableIncome$X1 Martin老師 @tpemartin c('年分','地區','來台旅遊人數(萬)') -> names(graphData$travelerFromAsia)
asus50901
@asus50901
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()
Martin老師
@tpemartin
Martin老師
@tpemartin
names(graphData$data) <- c("年份", "國民所得儲蓄投資毛額", "毛額") Martin老師 @tpemartin Martin老師 @tpemartin data.frame( x=x, xinterval=x_interval ) -> df_x df_x %>% View Hsinyung @Hsinyung 老師，我想請問一下，圖例的標題要怎麼改呢？ 我上網查了很多種打法都沒用 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("運輸工具"))
這是我目前的打法
Hsinyung
@Hsinyung
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_fill這串也不行
Martin老師
@tpemartin
只有aes(...)的才會有圖例，所以你只能針對x, y, color去產生圖例
asus50901
@asus50901
Legend.title 是放在theme()裡面的
Labs只能放 title subtitle caption x y
asus50901
@asus50901
你的ggplot(aes())裡面沒有寫fill 所以scale_fill_discrete 會抓不到fill啥
問題應該出在這 不過最好要看error 回傳什麼
Hsinyung
@Hsinyung
好的，謝謝
Martin老師
@tpemartin
lengend會對應某個aes mapping variable, 要改它要從此aes的scale_函數下手。假設是aes mapping variable是這裡的color，因此要去scale_color改; 你用的是scale_color_discrete_sequential(), 所以在那設定。在每個scale_...()下有一個設定為guide，你要給它一個list of guide setting ，一般透過guide_lengend(), 所以這裡你要用：
scale_color_discrete_sequential(...., guide=guide_legend(***))
*的設定可以看https://ggplot2.tidyverse.org/reference/guide_legend.html
asus50901
@asus50901
# Run one codeline at a time

if(!require(installr)) {
install.packages("installr");
require(installr)
}

updateR()

update.packages(ask = FALSE, checkBuilt =  TRUE)

asus50901
@asus50901
options(scipen = 99) # 去除科學記號
asus50901
@asus50901
library(readr)

north <- df_geo_northTW %>% filter(COUNTYNAME == "新北市")

northTW <-ggplot() +
geom_polygon(
data=north,
aes(x=x,y=y)
)

northTW
shuyulin97
@shuyulin97
df_geo_northTW %>%
filter(COUNTYNAME == "新北市") %>%
ggplot(aes(x = x, y = y))+geom_polygon()
asus50901
@asus50901
ggthemes:::theme_economist
getAnywhere(theme_economist)
View(ggtheme::theme_economist)`