R语言基于ggplot2绘图细节调整_07Jul2020

缘起

学习技术的一个方法就是直接接一个项目,实现它,掌握之。秉承这种想法,我尝试复刻了 Prevalence, awareness, treatment, and control of hypertension in China: data from 1·7 million adults in a population-based screening study (China PEACE Million Persons Project) 这个文献中的图形。

目的

图形不难,但是使用代码控制绘图的一些细节是值得记录的,以后需要时可以当作笔记来看一些参数的设置。

原图形

蒋图1.png
蒋图2.png

R语言基于ggplot2实现

library(ggplot2)
library(grid)
library(reshape2)
#Prevalence, awareness, treatment, and control of hypertension in China
#figure 1
agegrp <- c("35-39", "40-44", "45-49", "50-54", 
            "55-59", "60-64", "65-69", "70-74")
prevalence_men <- c(22, 31, 40, 44, 53, 58, 60, 62)
awareness_men <- c(28, 29, 33, 42, 45, 47, 50, 53)
treatment_men <- awareness_men - 5
control_men <- c(4, 5, 6, 7, 8, 9, 10, 11)

prevalence_women <- prevalence_men - 2
awareness_women <- awareness_men + 2
treatment_women <- treatment_men + 2
control_women <- control_men

gender <- rep(c("men", "women"), each = 8)

df <- data.frame(agegrp = factor(rep(agegrp,2)), 
                 gender = factor(gender), 
                 awareness = c(awareness_men, awareness_women), 
                 prevalence = c(prevalence_men, prevalence_women), 
                 treatment = c(treatment_men, treatment_women), 
                 control = c(control_men, control_women)
)

gph_men_prevelence <- ggplot(df[df$gender == "men",], aes(x = agegrp, y=prevalence)) + 
  geom_bar(stat = "identity", fill = "#99CC66", color = "black", width = .8) + 
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0), breaks=seq(0, 100, 10)) +
  theme(axis.title.y = element_text(size = 10, color = "black")) +
  theme(axis.text.x = element_blank()) + 
  theme(axis.text.y = element_text(size = 10)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(size = .5, colour = "black")) + 
  labs(x = "", y = "Participants(%)", title = "Men") + 
  theme(plot.title = element_text(size = 10, color = "black", face = "bold"))

gph_women_prevelence <- ggplot(df[df$gender == "women",], aes(x = agegrp, y=prevalence)) + 
  geom_bar(stat = "identity", fill = "#99CC66", color = "black", width = .8) + 
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0), breaks=seq(0, 100, 10)) +
  theme(axis.title.y = element_text(size = 10, color = "white")) +
  theme(axis.text.x = element_blank()) + 
  theme(axis.text.y = element_text(size = 10)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(size = .5, colour = "black")) + 
  labs(x = "", y = "", title = "Women") + 
  theme(plot.title = element_text(size = 10, color = "black", face = "bold"))

df2 <- subset(df, select = c(agegrp, gender, awareness, treatment, control)); df2
df3 <- melt(df2, id = c("agegrp", "gender"))
df4 <- subset(df3, gender == "men"); df4

gph_men_3 <- ggplot(df4, aes(x = agegrp, y=value, fill = variable)) + 
  geom_bar(stat = "identity", color = "black", position = "dodge") + 
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0), breaks=seq(0, 100, 10)) +
  theme(axis.title.y = element_text(size = 10, color = "black")) +
  theme(axis.title.x = element_text(size = 10, color = "black")) +
  theme(axis.text.x = element_text(size = 10, color = "black", angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(size = 10)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(size = .5, colour = "black")) + 
  labs(x = "Age(years)", y = "Participants(%)", title = "Men") + 
  theme(plot.title = element_text(size = 10, color = "black", face = "bold")) + 
  theme(legend.position = c(0.02, 1), legend.justification = c(0, .8)) +
  theme(legend.key.size=unit(4, "mm")) + 
  scale_fill_manual(values=c("#ADDAFA", "#40B0FF", "#0061A6"), 
                    name=" ",
                    breaks=c("awareness", "treatment", "control"),
                    labels=c("Awareness", "Treatment", "Control"))

df4 <- subset(df3, gender == "women"); df4

gph_women_3 <- ggplot(df4, aes(x = agegrp, y=value, fill = variable)) + 
  geom_bar(stat = "identity", color = "black", position = "dodge") + 
  scale_y_continuous(limits = c(0, 100), expand = c(0, 0), breaks=seq(0, 100, 10)) +
  theme(axis.title.y = element_text(size = 10, color = "white")) +
  theme(axis.title.x = element_text(size = 10, color = "black")) +
  theme(axis.text.x = element_text(size = 10, color = "black", angle = 45, hjust = 1)) +
  theme(axis.text.y = element_text(size = 10)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(size = .5, colour = "black")) + 
  labs(x = "Age(years)", y = "", title = "Women") + 
  theme(plot.title = element_text(size = 10, color = "black", face = "bold")) + 
  theme(legend.position = "none") + 
  scale_fill_manual(values=c("#ADDAFA", "#40B0FF", "#0061A6"), 
                    name=" ",
                    breaks=c("awareness", "treatment", "control"),
                    labels=c("Awareness", "Treatment", "Control"))


pushViewport(viewport(layout = grid.layout(2,2))) 
vplayout <- function(x,y){
  viewport(layout.pos.row = x, layout.pos.col = y)
}
print(gph_men_prevelence, vp = vplayout(1,1))   
print(gph_women_prevelence, vp = vplayout(1,2))   
print(gph_men_3, vp = vplayout(2,1))
print(gph_women_3, vp = vplayout(2,2))   

#figure2
dev.new()
library(ggplot2)
library(grid)

set.seed(1234)
data = (rnorm(500) + 10 ) * 10
grpvar = rep(c("D", "E", "F", "G"), length = 500)
df <- data.frame(data = data, grpvar = grpvar)
df$data[df$grpvar == "E"] <- df$data - 10
df$data[df$grpvar == "F"] <- df$data - 20
df$data[df$grpvar == "G"] <- df$data - 30

gph_density <- ggplot(data = df, aes(x = data, fill = grpvar)) + 
  geom_density(alpha = .7) + 
  scale_y_continuous(limits = c(0, .05), expand = c(0, 0)) +
  scale_x_continuous(limits = c(0, 150), expand = c(0, 0), breaks = seq(0, 150, 25)) +
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(panel.border = element_blank()) + 
  theme(axis.line = element_line(size = .5, colour = "black")) + 
  guides(fill = guide_legend(reverse=TRUE)) +
  theme(legend.position = c(0.8, 1), legend.justification = c(0,1)) +
  scale_fill_manual(values = c("#856BF8", "#FFB061", "#04B3DC", "#FE6170"),
                    name="Age(year) ",
                    breaks=c("D", "E", "F", "G"),
                    labels=c("65-75", "55-64", "45-54", "35-44")) +
  labs(x = "Awareness(%)", y = "  \n Estimatied Probalbility density", title = "") +
  theme(legend.key.size=unit(4, "mm"))

pushViewport(viewport(layout = grid.layout(3,3))) 
vplayout <- function(x,y){
  viewport(layout.pos.row = x, layout.pos.col = y)
}
print(gph_density, vp = vplayout(1,1))   
print(gph_density, vp = vplayout(1,2))   
print(gph_density, vp = vplayout(2,1))
print(gph_density, vp = vplayout(2,2))
print(gph_density, vp = vplayout(3,1))
print(gph_density, vp = vplayout(3,2))
print(gph_density, vp = vplayout(1,3))
print(gph_density, vp = vplayout(2,3))
print(gph_density, vp = vplayout(3,3))



#Availability, cost, and prescription patterns of antihypertensive medications
#figure2
library("ggplot2")
library("Cairo")

df <- data.frame(
  x = c(1, 3, 1, 3),
  y = c(1.4, 3.4, 3.4, 1.4),
  area = c(53.8, 7.8, 32.7, 5.7),
  color = factor(c("a", "b", "c", "b"))
)

df <- transform(df, radius = 3 * area^(1/2)); df

CairoSVG(file="JiangLixin.svg",width=6,height=6)
ggplot(data = df, aes(x = x, y = y, size = radius, color = color)) + 
  geom_point() + 
  scale_y_continuous(limits = c(0, 4), expand = c(0, 0)) +
  scale_x_continuous(limits = c(0, 4), expand = c(0, 0)) +
  theme_bw() + 
  theme(panel.grid.minor = element_blank()) +
  theme(panel.grid.major = element_blank()) + 
  theme(panel.background = element_blank()) +
  theme(axis.line = element_blank()) + 
  theme(axis.text = element_blank()) +
  theme(axis.ticks = element_blank()) + 
  theme(legend.position = "none") + 
  labs(x = "", y = "", title = "") +
  scale_size_identity(df$radius) + 
  geom_hline(yintercept = 2) +
  geom_vline(xintercept = 2) + 
  annotate("text", x = 1, y = .7, label = "53.8%\n(53·2–54·4)", size = 4.5) + 
  annotate("text", x = 1, y = 2.7, label = "32.7%\n(32·2–33·3)", size = 4.5) +
  annotate("text", x = 3, y = .7, label = "write\nyour number", size = 4.5) + 
  annotate("text", x = 3, y = 2.7, label = "write\nyour number", size = 4.5) 
dev.off()
  

效果

蒋图ggplot2实现.png

你可能感兴趣的:(R语言基于ggplot2绘图细节调整_07Jul2020)