我需要獲得在 ggplot2 中看起來一致的圖表。該腳本每次運行回傳大約 500 個圖形,因此不能手動更改它。
我的兩個問題是,第一,當標簽太長時,圖形變小(如圖 A 所示);第二,當我有更多條件(更多條形)時,條形變窄(如圖 B 所示)。我是初學者,所以我需要你的建議,因為我在網上找不到任何東西。另外,劇本不是我的。它來自一個前同事,我正在努力改進它。它有 300 行長,所以我想我不能把它貼在這里。

uj5u.com熱心網友回復:
操作。在沒有代碼的情況下幫助處理您的特定情況有點困難,但這里有一個建議,說明如何保持繪圖的高度一致,因為 x 軸上的名稱存在一些變化。
關于當您有更多條件時縮小列的問題的一部分......這將發生。作為替代方案,您希望發生什么?如果您可以指定,我們可以提供幫助 - 可以將其作為一個單獨的問題,并提供一個可重復的示例。
這是一個可重現的示例:
library(ggplot2)
set.seed(8675309)
df <- data.frame(
x = rep(c("A short label", "A long long long long label", "A medium-sized label here"), each=3),
categories = rep(LETTERS[1:3], 3),
y = sample(1:100, 9)
)
p <-
ggplot(df, aes(x=x, y=y, fill=categories))
geom_col(position=position_dodge(0.6), width=0.6, color='black')
scale_y_continuous(expand=expansion(mult=c(0, 0.05)))
xlab("This is the X Axis")
theme_classic()
theme(
axis.text.x = element_text(angle=35, hjust=1, vjust=1)
)
p

根據該代碼,圖上條形底部到軸標題的距離將根據軸上文本的長度而變化。解決這種不一致外觀的一種方法是,如果文本長于特定的最大長度,則強制文本換行到下一行。我將使用scales包來做到這一點:
library(scales)
# force text longer than 15 characters to wrap to the next line
p scale_x_discrete(labels=label_wrap(15))

只要您沒有圖表,其中x 軸標簽上的所有內容都在15 個字符以下……那就行了。您可能需要使用精確數量的字符來強制換行。
uj5u.com熱心網友回復:
為了縮小我的列,我想縮小我的整體圖的大小。如下圖所示,當我繪制 5 個條件時,我得到圖形 A(列大小為“紫線”。當我繪制 4 個條件或更少時,我得到與 A 具有不同列大小的圖形 B(請參閱相同的“紫線”現在比列小。我想得到圖 C,它與 A 具有相同的列大小,但它更小(它只是缺少一個列組)。
數字
我將留下我繪制圖形的腳本部分:
塊參考
###############################################################################
# Part 1: Initialize the variables and setup the general variables (e.g. colors).
figures_case_1 <- list()
Controls_case_1 <- list()
ii <- 1
colors <- c('gray85','green3', 'firebrick1')
text_size <- 20
# Part 2: Create the table with the information to plotted.
for(l in 2:length(all_dataframes)){
splitted_doses_first <- unlist(strsplit(as.character(all_dataframes[l][[1]][1,"Dose"]),"\\s"))
# Initialize the dataframe
combined_dataFrame <- data.frame(matrix(ncol = 6, nrow = 0))
x <- c("Activity", "Dose", "STD", "Cell_Population","Drug","dose_number")
colnames(combined_dataFrame) <- x
if(length(splitted_doses_first) > 2){
# Entro a este loop cuando tengo mas de una droga. Desde dos combinaciones en adelante.
for(j in 2:length(all_dataframes)){
splitted_doses_second <- unlist(strsplit(as.character(all_dataframes[j][[1]][1,"Dose"]),"\\s"))
if(length(splitted_doses_second) == 2){
# Entro a este loop cuando tenga una droga individual.
intersection <- intersect(splitted_doses_first,splitted_doses_second)
idx <- which(intersection == ' ')
if(length(idx) != 0){intersection <- intersection[-idx]}
if(length(intersection) == 2 && str_contains(as.character(all_dataframes[l][[1]][1,"Dose"]),
as.character(all_dataframes[j][[1]][1,"Dose"]))){
aux <- all_dataframes[j][[1]]
combined_dataFrame <- rbind(aux, combined_dataFrame)
}
}
}
aux <- all_dataframes[1][[1]] # Add DMSO.
combined_dataFrame <- rbind(aux, combined_dataFrame)
# Part 3: Order the table.
combined_dataFrame$dose_number <- as.numeric(as.character(combined_dataFrame$dose_number))
combined_dataFrame <- combined_dataFrame[order(combined_dataFrame["dose_number"]),]
row.names(combined_dataFrame) <- NULL
combined_dataFrame$Dose <- factor(combined_dataFrame$Dose, levels = unique(combined_dataFrame$Dose))
combined_dataFrame <- rbind(combined_dataFrame, all_dataframes[l][[1]])
Controls_case_1[[ii]] = combined_dataFrame
# Part 4: Create the figure.
title_fig <- as.character(all_dataframes[l][[1]][1,"Drug"])
figure <- ggplot(data = combined_dataFrame, aes(x=as.factor(Dose), y=Activity, fill=Cell_Population))
geom_hline(yintercept=25, linetype="solid", colour = "grey86", size=0.5)
geom_hline(yintercept=50, linetype="solid", colour = "grey86", size=0.5)
geom_hline(yintercept=75, linetype="solid", colour = "grey86", size=0.5)
geom_hline(yintercept=125, linetype="solid", colour = "grey86", size=0.5)
geom_hline(yintercept=150, linetype="solid", colour = "grey86", size=0.5)
geom_bar(stat="identity", color="black",
width = 0.8, position = position_dodge(width = 0.9))
geom_errorbar(aes(ymin=Activity-STD, ymax=Activity STD), width=.2, alpha=0.9, size=0.5,
position=position_dodge(.9))
coord_cartesian(ylim = c(0, 150))
scale_y_continuous(breaks=c(0,25,50,75,100,125,150))
# Counts: y_axes="Events" // Normalized: y_axes = "Survival (%)" // // OnlyDMSO: x_axes= Cell_Population.
labs(title=paste(exp," OP#", patient," \n", title_fig), y = "Survival (%)", x = "Doses (nM)")
theme(
# panel.grid.major = element_line(colour = "gray48"), #LINEAS DE FONDOS
panel.background = element_rect(fill = "white"), #COLOR DE FRáFICA FONDO # plot.margin = margin(2, 2, 2, 2, "cm"),
plot.background = element_rect(
fill = "white",
colour = "white",
size = 0.1),
plot.title = element_text(hjust = 0.5, size = text_size * 1.3, face = "bold"),
axis.text.x = element_text(size=text_size,angle=30, hjust=0.5, vjust=0.5, face = "bold"),
axis.text.y = element_text(size=text_size, face = "bold"),
axis.title = element_text(size=text_size * 1.1, face = "bold"),
legend.text= element_text(size=text_size * 1.1, face = "bold"),
legend.title = element_text(size=text_size * 1.1, face = "bold"))
geom_hline(yintercept=0, linetype="solid", colour = "black", size=0.1)
geom_segment(aes(x = 0, y = 0, xend = 0, yend = 150))
geom_hline(yintercept=100, linetype="dashed", colour = "black", size=1)
scale_fill_manual(values=colors)
# Part 5: Saving independently all the figures.
figures_case_1[[1]] = figure
g <- grid.arrange(grobs = figures_case_1, nrow = 1 ,ncol = 1,gp=gpar(fontsize=2))
ggsave(paste(ii,"-",exp,"-",norm_count," ", title_fig,'.png',sep=""), g,
device = png , path = path_CYT_tables,
width = 12, height = 6,limitsize = FALSE)
ii <- ii 1
} }
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328492.html
上一篇:在旋轉的三角矩陣旁邊添加顏色條
