美文网首页
ggplot2: 绘图系列

ggplot2: 绘图系列

作者: MJades | 来源:发表于2020-04-05 17:01 被阅读0次
  • 矩形geom_rect()
df <- data.frame(matrix(nrow=5, ncol = 2))
names(df) <- c("variable", "percentage")
df$variable <- c("Carbohydrates", "Warming", "NGTnotPresent",   "DrainNotPresent", "DrEaMing")
df$percentage <- c(0.67,0.33,0.86,0.78,0.58)

df <- df %>% mutate(group=ifelse(percentage <0.6, "red",
                             ifelse(percentage>=0.6 & percentage<0.8,   "orange","green")),
                label=paste0(percentage*100, "%"),
                title=dplyr::recode(variable, `Carbohydrates`="Preoperative\ncarbohydrate loading",
                                    `Warming`="Intraoperative\nwarming",
                                    `NGTnotPresent`="Patients without a\nnasogastric tube\non arrival in recovery",
                                    `DrainNotPresent`="Patients without an\nabdominal drain\non arrival in recovery",
                                    `DrEaMing`="Patients DrEaMing on\npostoperative day 1"))

ggplot(df, aes(fill = group, ymax = percentage, ymin = 0, xmax = 2, xmin = 1))     +
  geom_rect(aes(ymax=1, ymin=0, xmax=2, xmin=1), fill ="#ece8bd") +
  geom_rect() + 
  coord_polar(theta = "y",start=-pi/2) + xlim(c(0, 2)) + ylim(c(0,2)) +
  geom_text(aes(x = 0, y = 0, label = label, colour=group), size=6.5,   family="Poppins SemiBold") +
  geom_text(aes(x=1.5, y=1.5, label=title), family="Poppins Light", size=4.2) + 
  facet_wrap(~title, ncol = 5) +
  theme_void() +
  scale_fill_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188")) +
  scale_colour_manual(values = c("red"="#C9146C", "orange"="#DA9112", "green"="#129188")) +
  theme(strip.background = element_blank(), strip.text.x = element_blank()) +
  guides(fill=FALSE) +
  guides(colour=FALSE)
Figure 1. 半圆图
Figure 2. 不加极坐标
  • segment绘图
library(ggplot2)
library(gglayer)
set.seed(2019-06-28)
d = data.frame(x = rnorm(10),
               xend = rnorm(10),
               y = rnorm(10),
               yend = rnorm(10),
               v1 = rnorm(10),
               v2 = rnorm(10))

ggplot(d) + 
  geom_segment_c(aes(x = x, xend = xend, y=y, yend =yend, col0 = v1, col1 =    v2)) +
  coord_fixed(ratio = 1)+
  scale_color_viridis_c(name = "continuous colored lines") + 
  theme_minimal() + 
  theme(legend.position=c(.2, .85)) +
  xlab(NULL) + ylab(NULL)
Figure 3. 带颜色的line
  • 分类树
# install.packages("remotes")
# remotes::install_github("grantmcdermott/parttree")
library(parsnip)
library(titanic) ## Just for a different data set
library(parttree)
set.seed(123) ## For consistent jitter

titanic_train$Survived = as.factor(titanic_train$Survived)

## Build our tree using parsnip (but with rpart as the model engine)
 ti_tree =
  decision_tree() %>%
  set_engine("rpart") %>%
  set_mode("classification") %>%
  fit(Survived ~ Pclass + Age, data = titanic_train)

## Plot the data and model partitions
titanic_train %>%
  ggplot(aes(x=Pclass, y=Age)) +
  geom_jitter(aes(col=Survived), alpha=0.7) +
  geom_parttree(data = ti_tree, aes(fill=Survived), alpha = 0.1) +
  theme_minimal()
Figure 4. 没加geom_parttree之前
Figure 5. parttree

相关文章

网友评论

      本文标题:ggplot2: 绘图系列

      本文链接:https://www.haomeiwen.com/subject/yfruphtx.html