1 Chuẩn bị dữ liệu

ecd=c(5.9,4.8,3.6,2.1,1.1,0.46,0.31)
cumwei=c(100,88.55,76.12,59.7,40.3,23.88,10.45)
data=data.frame(ecd,cumwei)
data
   ecd cumwei
1 5.90 100.00
2 4.80  88.55
3 3.60  76.12
4 2.10  59.70
5 1.10  40.30
6 0.46  23.88
7 0.31  10.45

2 Gọi các gói lệnh

Trong đoạn code này sử dụng 3 gói package sau: ggplot2,scalesplotly. Nếu máy chưa có thì cần phải cài trước, rồi mới thực hiện các lệnh dưới đây

install.packages('ggplot2')
install.packages('scales')
install.packages('plotly')

3 Đoạn code chính

Để đổi trục sang dạng log(x) ta có thể dùng lệnh coord_trans(x='log10')hoặc scale_x_log10(). Và nghịch đảo đồ thị ta dùng lệnh scale_x_continuous(trans = "reverse") có sẵn trong gói ggplot2. Nhưng hiện tại chưa biết cách phối hợp nên đành nhờ gói lệnh scales với hướng dẫn viết hàm reverselog tại Link

library(ggplot2)
#ham dung de ve do thi theo log va dao nguoc do thi lai
library(scales)
reverselog_trans <- function(base = exp(1)) {
    trans <- function(x) -log(x, base)
    inv <- function(x) base^(-x)
    trans_new(paste0("reverselog-", format(base)), trans, inv, 
              log_breaks(base = base), 
              domain = c(1e-100, Inf))
}
########################################
t <- ggplot(data, aes(ecd, cumwei))
t = t + geom_point(shape = 21, fill = "red", color="red", size=4) + 
    theme_bw() + 
    xlab ("ECD stage") +
    ylab("Cumulative Weight (%)") +
    ggtitle("PCD GROUP1")
#thiet lap thanh ti le cho truc x thanh log10(x)
t=t + scale_x_continuous(trans=reverselog_trans(10), limits = c(10,0.1), 
    breaks = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,2,3,4,5,6,7,8,9,10),
    labels = c('0.1','.2','.3','.4','.5','.6','.7','.8','.9',1,2,3,4,5,6,7,8,9,10))
# thiet lap ti le cho truc Y
t= t + scale_y_continuous(breaks = seq(0,100,10))
# ve duong tuyen tinh
t= t + geom_smooth(method='lm', se=F)
#doi mau grid sang mau gray
t= t + theme(panel.grid.major= element_line(color = "gray"))

#plot do thi len
#
library(plotly)
ggplotly(t)