英文:
axis style in ggplot2
问题
I cannot fulfill your request to translate the code part without providing any other content. If you have any other non-code related text that you would like me to translate, please feel free to provide that.
英文:
I want to apply the axis style in this post
https://stackoverflow.com/questions/10808761/r-style-axes-with-ggplot#
I did well with the y-axis but not with the x-axis because my x-axis was as a factor not a continues
here is the code I use
TKV_İmage <- ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
scale_y_continuous(name = "Viscosity (cP)", labels = label_number(accuracy = 0.01)) +
scale_x_discrete(name = "Shear rate (1/s)", labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM", "5.0 RPM", "10.0 RPM", "15.0 RPM", "20.0 RPM", "25.0 RPM", "40.0 RPM", "60.0 RPM")) +
scale_fill_manual(values = c("#4271AE", "#FF5A5F"), labels = c("Low Risk", "High Risk")) +
theme_bw() +
theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 45, hjust = 1),
axis.text.y = element_text(colour = "grey20", size = 12, vjust = 0.4),
axis.title = element_text(size = 14),
legend.title = element_blank(),
legend.position = "top",
legend.text = element_text(size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
base_breaks_y(PE_plot5$TKV_viscosity)
it's working fine but it doesn't show the X-axis
when I am adding this
+
base_breaks_x(PE_plot5$factor(TKV))
throws this error
> Error in pretty(x) : attempt to apply non-function
off course because the TKV is a factor I think
any help with this issue?
答案1
得分: 0
以下是您要翻译的内容:
"由于您的 x 轴变量是一个因子,您需要根据您引用的问题来调整代码,通过使用例如 scale_x_discrete
而不是 scale_x_continuous
,以及计算 min
和 max
的分段。另外,我添加了一个 ...
参数,允许传递更多参数,如 name
和 labels
给 scale
。
使用一些虚假的示例数据:
library(ggplot2)
library(scales)
PE_plot5 <- data.frame(
TKV = c("A", "B", "C"),
TKV_viscosity = 1:12,
Risk_Group = rep(c("a", "b"), each = 3)
)
base_breaks_x_discrete <- function(x, ...) {
b <- factor(x)
bmin <- min(as.numeric(b))
bmax <- max(as.numeric(b))
d <- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
list(
geom_segment(
data = d, aes(x = x, y = y, xend = xend, yend = yend),
inherit.aes = FALSE
),
scale_x_discrete(breaks = levels(b), ...)
)
}
base_breaks_y <- function(x, ...) {
b <- pretty(x)
d <- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
list(
geom_segment(
data = d, aes(x = x, y = y, xend = xend, yend = yend),
inherit.aes = FALSE
),
scale_y_continuous(breaks = b, ...)
)
}
ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
scale_fill_manual(
values = c("#4271AE", "#FF5A5F"),
labels = c("Low Risk", "High Risk")
) +
theme_bw() +
theme(
axis.text.x = element_text(
colour = "grey20",
size = 12, angle = 45, hjust = 1
),
axis.text.y = element_text(
colour = "grey20",
size = 12, vjust = 0.4
),
axis.title = element_text(size = 14),
legend.title = element_blank(),
legend.position = "top",
legend.text = element_text(size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) +
base_breaks_y(PE_plot5$TKV_viscosity,
name = "Viscosity (cP)",
labels = label_number(accuracy = 0.01)
) +
base_breaks_x_discrete(PE_plot5$TKV,
name = "Shear rate (1/s)",
labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM")
)
"
英文:
As your x axis variable is a factor you have to adapt the code from the question you referenced to account for that by using e.g. scale_x_discrete
instead of scale_x_continuous
as well as the computation of the min
and max
breaks. Additionally I added a ...
argument which allows to pass further arguments like name
and labels
to the scale
.
Using some fake example data:
library(ggplot2)
library(scales)
PE_plot5 <- data.frame(
TKV = c("A", "B", "C"),
TKV_viscosity = 1:12,
Risk_Group = rep(c("a", "b"), each = 3)
)
base_breaks_x_discrete <- function(x, ...) {
b <- factor(x)
bmin <- min(as.numeric(b))
bmax <- max(as.numeric(b))
d <- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
list(
geom_segment(
data = d, aes(x = x, y = y, xend = xend, yend = yend),
inherit.aes = FALSE
),
scale_x_discrete(breaks = levels(b), ...)
)
}
base_breaks_y <- function(x, ...) {
b <- pretty(x)
d <- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
list(
geom_segment(
data = d, aes(x = x, y = y, xend = xend, yend = yend),
inherit.aes = FALSE
),
scale_y_continuous(breaks = b, ...)
)
}
ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
scale_fill_manual(
values = c("#4271AE", "#FF5A5F"),
labels = c("Low Risk", "High Risk")
) +
theme_bw() +
theme(
axis.text.x = element_text(
colour = "grey20",
size = 12, angle = 45, hjust = 1
),
axis.text.y = element_text(
colour = "grey20",
size = 12, vjust = 0.4
),
axis.title = element_text(size = 14),
legend.title = element_blank(),
legend.position = "top",
legend.text = element_text(size = 12),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) +
base_breaks_y(PE_plot5$TKV_viscosity,
name = "Viscosity (cP)",
labels = label_number(accuracy = 0.01)
) +
base_breaks_x_discrete(PE_plot5$TKV,
name = "Shear rate (1/s)",
labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM")
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论