R Knitr Kable и GGPlot на одном ряду - PullRequest
0 голосов
/ 03 февраля 2020

Я все еще пытаюсь получить представление о latex / knitr / rmarkdown, однако я создал таблицу knitr (kable) и нашел учебник по созданию кругового гистограммы с использованием ggplot.

Однако я не могу понять, как сделать их рядом и на одной странице.

Если у вас есть какие-либо предложения о том, как это упростить, я хотел бы их увидеть, однако это то, с чем я начал работать.

На заметке, как бы я покрасил столбцы в гистограмме в соответствии с определенным значением? Возможно ли, чтобы цвет начинался с зеленого в нижней части, затем go до оранжевого, а затем, наконец, красного?

<<ome-table, results="asis", echo = FALSE, out.width='\\textwidth', fig.show='hold'>>=
Omes.Name <- c("Ome 1", "Ome 2", "Ome 3", "Ome 4", "Ome 5", "Ome 6")
Report.Omes <- c(12.11, 10.55, 13.98, 1.23, 5.45, 2.87)
names(Report.Omes) <- Omes.Name
Omes.Upper <- c(14.11, 11.55, 12.98, 2.23, 5.45, 2.9)
names(Omes.Upper) <- Omes.Name
fold.change <- Report.Omes/Omes.Upper

bad.folds <- which(fold.change > 3)
warning.folds <- which(fold.change > 1 & fold.change < 3)
good.folds <- which(fold.change < 1)

Omes.df <- data.frame(round(Report.Omes, 1), round(fold.change, 1), stringsAsFactors = F)
names(Omes.df) <- c("Sample Omes", "Fold Change")

tab1 <- Omes.df %>%
  kable("latex", booktabs = T, linesep = "") %>%
  kable_styling() %>%
  row_spec(0, bold = T, color = "black") %>%
  row_spec(bad.folds, bold = T, color = "black", background = "#ecb1b3") %>%
  row_spec(warning.folds, bold = T, color = "black", background = "#ebc8b2") %>%
  row_spec(good.folds, bold = T, color = "black", background = "#b3e7b6")


data <- data.frame(
  id=seq(1,length(Omes.Name)),
  individual=Omes.Name,
  value=ifelse(fold.change>10,10,fold.change)
)

# Get the name and the y position of each label
label_data <- data

# calculate the ANGLE of the labels
number_of_bar <- nrow(label_data)
angle <-  90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)

# calculate the alignment of labels: right
label_data$hjust<-ifelse( angle < -90, 1, 0)    # If I am on the left part of the plot, my labels have currently an angle < -90

# flip angle BY to make them readable
label_data$angle<-ifelse(angle < -90, angle+180, angle)
# ----- ------------------------------------------- ---- #

p <- ggplot(data, aes(x=as.factor(id), y=value*10)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
  geom_bar(stat="identity", fill=alpha("skyblue", 0.7)) +  # This add the bars with a blue color
  #scale_fill_manual(values = c("#bc0000", "#bc4b00", "#0043B5", "#01b504")) 
  ylim(-15,100) +      # Limits of the plot = very important. The negative value controls the size of the inner circle, the positive one is useful to add size over each bar
  theme_minimal() +    # Custom the theme: no axis title and no cartesian grid
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm")      # Adjust the margin to make in sort labels are not truncated!
  ) +

  coord_polar(start = 0) +    # This makes the coordinate polar instead of cartesian.

  # Add the labels, using the label_data dataframe that we have created before
  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE )

cat(tab1)
p
@
...