iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 30
0
自我挑戰組

資料處理以及視覺化系列 第 30

R語言-第三十天 Shiny - 繪製常態分配圖 - 區域上色、標上特定點

最後一篇

目標在常態圖上標上我的目標點

假設這一點 z值是1.96

a1 = 1.96
a2 = dnorm(1.96)
t1 = data.frame(x = a1 , y = a2)

接下來就將上方的data.frame的點加入到前方的ggplot中

ggplot(data.frame(x = random , y = density),aes(x = random , y = density)) +  
  geom_line(size = 1) +
  geom_point(data = t1,aes(x = a1, y = a2),col = "red" , size = 3)+
  labs(x = "Z值" , y = "機率分佈")+
  theme(panel.background=element_rect(fill='white', color="white",col = "white"))+## #cc9999背景顏色
  theme(plot.background = element_rect("white"))

那如果要加上範圍的話
可以使用geom_polygon來做圖
首先先設計data.frame

pos_x = seq(from = min(random),to = a1 , by = .0001)
pos_y = dnorm(pos_x)

pos_com = data.frame(x = pos_x , y = pos_y)

pos_com = rbind(c(min(pos_x),0),pos_com,c(a1,0))

設計完之後 , 同樣的把data.frame加到ggplot中

ggplot(data.frame(x = random , y = density),aes(x = random , y = density)) +  
  geom_polygon(data = pos_com , aes(x = x , y = y))+
  geom_line(size = 1) +
  geom_point(data = t1,aes(x = a1, y = a2),col = "red" , size = 3)+
  labs(x = "Z值" , y = "機率分佈")+
  theme(panel.background=element_rect(fill='white', color="white",col = "white"))+## #cc9999背景顏色
  theme(plot.background = element_rect("white"))

https://ithelp.ithome.com.tw/upload/images/20191014/20112574Cf4RwwJyYC.png

完成!!!

終於寫完30篇了......
今年真的感到異常得疲倦阿.....XD

但還是恭喜我自己堅持到底拉!!

yaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa


上一篇
R語言-第二十九天 Shiny - 常態分配圖
系列文
資料處理以及視覺化30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言