ggplot 的问题 - 如何将 y-axis 标签设置为粗体,编辑图例标题并调整误差线的宽度
Problems with ggplot - how to set y-axis label to bold, edit legend title and adjust the width of error bars
我创建了一个 ggplot 函数来绘制下面给出的数据组(很抱歉,我不知道如何 post 数据文件)
此图存在三个问题:
1.The Y-axis 标题有一个上标字符。因此,它不能加粗(x-axis 标题的格式相同)
2.The 图例标题定义为 "Group ID"。但是代码使用列标题 "GroupID" 忽略了定义。 (代码行“scale_fill_discrete("Group ID")”应该设置图例标题)。
3.The误差线的宽度不是恒定的,它在点与点之间变化(绿色的很明显)
感谢您帮助解决这些问题。
代码:
library(ggplot2)
# Colors
Mycolors<- c("blue", "red", "green", "cyan", "orange", "brown", "magenta", "grey", "black")
# Shapes
Myshapes<-c(15:25,0:2)
# Plot position
Mypd <- position_dodge(width = 0.2)
# Read data
mData<-read.csv("F:/user/documents/R/Notebooks/Ggplot/TestData.csv")
pAllGroupData<-function(mData, xLabel, yLabel, gTitle, sTitle, sCaption, yType, gErrType) {
v <- mData$dMean
x <- mData$DayNum
p <- ggplot(data = mData, mapping = aes(x = x, y = v, color = GroupID, shape = GroupID))
p <- p + geom_line(position = Mypd) +
geom_point(position = Mypd, size = 4)
# Plot errorbars
if (gErrType == "StdErr") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dStdErr, ymax = v + mData$dStdErr), width=1.5, position=Mypd)
} else if (gErrType == "StdDev") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dStdDev, ymax = v + mData$dStdDev), width=1.5, position=Mypd)
} else if (gErrType == "IQR") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dIQR, ymax = v + mData$dIQR), width=1.5, position=Mypd)
}
# Turn Y axis logarithmic and place log ticks
if (yType == "Log") {
p<- p + ylim (1, vMax) + # This is to avoid log(0)
coord_trans(y = "log10") +
annotation_logticks(sides = "lr", scaled=FALSE) # ticks are only left and right y axes
}
# Plot the graph with theme
p <- p +
labs(x = xLabel, y = yLabel, title = gTitle, subtitle = sTitle, caption = sCaption) +
# Include origine (0, 0)
expand_limits(x = 0, y = 0) +
# Custom Colors
scale_colour_manual(values = Mycolors) +
# Custom Shapes
scale_shape_manual(values = Myshapes) +
# Legend Title (not working!)
scale_fill_discrete("Group ID")
p <- p + gTheme(p)
return(p)
}
# Test
p<-pAllGroupData(mData, xLabel = "Days", yLabel = bquote("Volume "~(mm^3)), gTitle = "Study", sTitle = "X", sCaption = "SCaption", yType = "Lin", gErrType = "StdDev")
p
数据:
GroupID DayNum n dMean dMedian dStdDev dStdErr dIQR
Grp1 13 8 207.03 211.45 13.04 4.61 11.73
Grp1 15 8 288.15 274.40 48.98 17.32 33.25
Grp1 18 8 393.50 381.15 63.63 22.50 52.98
Grp1 21 8 507.63 499.80 73.06 25.83 80.88
Grp1 26 8 636.14 614.65 112.53 39.79 206.53
Grp2 13 8 207.05 205.25 41.00 14.50 72.35
Grp2 15 8 142.76 145.60 27.87 9.85 33.70
Grp2 18 8 77.55 82.55 19.44 6.87 22.88
Grp2 21 8 66.38 69.85 20.56 7.27 23.00
Grp2 26 8 67.05 64.20 29.02 10.26 25.48
Grp2 29 8 66.48 63.85 25.95 9.17 19.38
Grp2 33 8 76.96 74.25 25.31 8.95 28.60
Grp3 13 8 207.94 219.65 34.42 12.17 47.18
Grp3 15 8 149.56 155.25 45.74 16.17 70.68
Grp3 18 8 134.83 128.00 59.10 20.90 66.20
Grp3 21 8 164.99 159.40 67.86 23.99 93.63
Grp3 26 8 149.53 160.05 62.48 22.09 100.58
Grp3 29 8 162.21 184.25 61.21 21.64 113.33
Grp3 33 8 177.19 184.00 68.99 24.39 110.35
Grp3 36 8 192.13 160.25 94.93 33.56 122.30
当您有多个问题时,建议将您的问题分成几个帖子。同时,这是我试图帮助你的尝试:
问题 1
将yLabel = bquote("Volume "~(mm^3))
替换为yLabel = bquote(bold("Volume " ~ (mm ^ 3)))
(注意bold
)。温馨提醒,此问题已在过去解决 here and also here
问题 2
您没有使用任何填充美学,因此删除 scale_fill_discrete("Group ID")
,这对您实际使用的每种美学(颜色和形状)没有任何影响,在 scale_colour_manual
和 scale_shape_manual
添加 name = "Group ID"
(您想要的图例标题):
scale_colour_manual(name = "Group ID", values = Mycolors) +
scale_shape_manual(name = "Group ID", values = Myshapes)
问题 3
这似乎已被一些 on GitHub, here 报告为一个问题(link 也包含一个解决方案)
关于 SO 的类似问题:ggplot2 position_dodge affects error bar width and Width of error bars in ggplot2,两者都有一些您可以探索的答案。
因此,您应该将误差条的宽度缩放到每个 DayNum
的行数。因此,在您的 data.frame 中创建一个新列,像这样说 width
:
library(dplyr)
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
然后您必须将此列映射到 ggplot
的 width
美学中,同时从 geom_errorbar
中删除任何 width
属性。
这是一个工作示例,其中包含您的数据,涵盖了所有 3 个问题。
您的数据:
mData <- read.table(
text = "
GroupID DayNum n dMean dMedian dStdDev dStdErr dIQR
Grp1 13 8 207.03 211.45 13.04 4.61 11.73
Grp1 15 8 288.15 274.40 48.98 17.32 33.25
Grp1 18 8 393.50 381.15 63.63 22.50 52.98
Grp1 21 8 507.63 499.80 73.06 25.83 80.88
Grp1 26 8 636.14 614.65 112.53 39.79 206.53
Grp2 13 8 207.05 205.25 41.00 14.50 72.35
Grp2 15 8 142.76 145.60 27.87 9.85 33.70
Grp2 18 8 77.55 82.55 19.44 6.87 22.88
Grp2 21 8 66.38 69.85 20.56 7.27 23.00
Grp2 26 8 67.05 64.20 29.02 10.26 25.48
Grp2 29 8 66.48 63.85 25.95 9.17 19.38
Grp2 33 8 76.96 74.25 25.31 8.95 28.60
Grp3 13 8 207.94 219.65 34.42 12.17 47.18
Grp3 15 8 149.56 155.25 45.74 16.17 70.68
Grp3 18 8 134.83 128.00 59.10 20.90 66.20
Grp3 21 8 164.99 159.40 67.86 23.99 93.63
Grp3 26 8 149.53 160.05 62.48 22.09 100.58
Grp3 29 8 162.21 184.25 61.21 21.64 113.33
Grp3 33 8 177.19 184.00 68.99 24.39 110.35
Grp3 36 8 192.13 160.25 94.93 33.56 122.30",
header = TRUE
)
一个工作示例:
library(dplyr)
library(ggplot2)
# Solves Problem 3 - create helper column width
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
Mypd <- position_dodge(width = 0.2)
ggplot(data = mData,
mapping = aes(x = DayNum,
y = dMean,
color = GroupID,
shape = GroupID,
width = width)) + # map width column to width aesthetic
geom_line(position = Mypd) +
geom_point(position = Mypd,
size = 2) +
# width is inherited from the above mapping, no need to map it again
geom_errorbar(aes(ymin = dMean - mData$dStdDev,
ymax = dMean + mData$dStdDev),
position = Mypd) +
labs(y = bquote(bold("Volume " ~ (mm ^ 3)))) + # Solves Problem 1
# Solves Problem 2:
scale_colour_manual(name = "Group ID", values = c("Grp1" = "blue",
"Grp2" = "red",
"Grp3" = "green")) +
scale_shape_manual(name = "Group ID", values = c("Grp1" = 15,
"Grp2" = 16,
"Grp3" = 17)) +
theme_bw()
我创建了一个 ggplot 函数来绘制下面给出的数据组(很抱歉,我不知道如何 post 数据文件)
1.The Y-axis 标题有一个上标字符。因此,它不能加粗(x-axis 标题的格式相同)
2.The 图例标题定义为 "Group ID"。但是代码使用列标题 "GroupID" 忽略了定义。 (代码行“scale_fill_discrete("Group ID")”应该设置图例标题)。
3.The误差线的宽度不是恒定的,它在点与点之间变化(绿色的很明显)
感谢您帮助解决这些问题。
代码:
library(ggplot2)
# Colors
Mycolors<- c("blue", "red", "green", "cyan", "orange", "brown", "magenta", "grey", "black")
# Shapes
Myshapes<-c(15:25,0:2)
# Plot position
Mypd <- position_dodge(width = 0.2)
# Read data
mData<-read.csv("F:/user/documents/R/Notebooks/Ggplot/TestData.csv")
pAllGroupData<-function(mData, xLabel, yLabel, gTitle, sTitle, sCaption, yType, gErrType) {
v <- mData$dMean
x <- mData$DayNum
p <- ggplot(data = mData, mapping = aes(x = x, y = v, color = GroupID, shape = GroupID))
p <- p + geom_line(position = Mypd) +
geom_point(position = Mypd, size = 4)
# Plot errorbars
if (gErrType == "StdErr") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dStdErr, ymax = v + mData$dStdErr), width=1.5, position=Mypd)
} else if (gErrType == "StdDev") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dStdDev, ymax = v + mData$dStdDev), width=1.5, position=Mypd)
} else if (gErrType == "IQR") {
p<- p + geom_errorbar(aes(x = x, ymin = v - mData$dIQR, ymax = v + mData$dIQR), width=1.5, position=Mypd)
}
# Turn Y axis logarithmic and place log ticks
if (yType == "Log") {
p<- p + ylim (1, vMax) + # This is to avoid log(0)
coord_trans(y = "log10") +
annotation_logticks(sides = "lr", scaled=FALSE) # ticks are only left and right y axes
}
# Plot the graph with theme
p <- p +
labs(x = xLabel, y = yLabel, title = gTitle, subtitle = sTitle, caption = sCaption) +
# Include origine (0, 0)
expand_limits(x = 0, y = 0) +
# Custom Colors
scale_colour_manual(values = Mycolors) +
# Custom Shapes
scale_shape_manual(values = Myshapes) +
# Legend Title (not working!)
scale_fill_discrete("Group ID")
p <- p + gTheme(p)
return(p)
}
# Test
p<-pAllGroupData(mData, xLabel = "Days", yLabel = bquote("Volume "~(mm^3)), gTitle = "Study", sTitle = "X", sCaption = "SCaption", yType = "Lin", gErrType = "StdDev")
p
数据:
GroupID DayNum n dMean dMedian dStdDev dStdErr dIQR
Grp1 13 8 207.03 211.45 13.04 4.61 11.73
Grp1 15 8 288.15 274.40 48.98 17.32 33.25
Grp1 18 8 393.50 381.15 63.63 22.50 52.98
Grp1 21 8 507.63 499.80 73.06 25.83 80.88
Grp1 26 8 636.14 614.65 112.53 39.79 206.53
Grp2 13 8 207.05 205.25 41.00 14.50 72.35
Grp2 15 8 142.76 145.60 27.87 9.85 33.70
Grp2 18 8 77.55 82.55 19.44 6.87 22.88
Grp2 21 8 66.38 69.85 20.56 7.27 23.00
Grp2 26 8 67.05 64.20 29.02 10.26 25.48
Grp2 29 8 66.48 63.85 25.95 9.17 19.38
Grp2 33 8 76.96 74.25 25.31 8.95 28.60
Grp3 13 8 207.94 219.65 34.42 12.17 47.18
Grp3 15 8 149.56 155.25 45.74 16.17 70.68
Grp3 18 8 134.83 128.00 59.10 20.90 66.20
Grp3 21 8 164.99 159.40 67.86 23.99 93.63
Grp3 26 8 149.53 160.05 62.48 22.09 100.58
Grp3 29 8 162.21 184.25 61.21 21.64 113.33
Grp3 33 8 177.19 184.00 68.99 24.39 110.35
Grp3 36 8 192.13 160.25 94.93 33.56 122.30
当您有多个问题时,建议将您的问题分成几个帖子。同时,这是我试图帮助你的尝试:
问题 1
将yLabel = bquote("Volume "~(mm^3))
替换为yLabel = bquote(bold("Volume " ~ (mm ^ 3)))
(注意bold
)。温馨提醒,此问题已在过去解决 here and also here
问题 2
您没有使用任何填充美学,因此删除 scale_fill_discrete("Group ID")
,这对您实际使用的每种美学(颜色和形状)没有任何影响,在 scale_colour_manual
和 scale_shape_manual
添加 name = "Group ID"
(您想要的图例标题):
scale_colour_manual(name = "Group ID", values = Mycolors) +
scale_shape_manual(name = "Group ID", values = Myshapes)
问题 3
这似乎已被一些 on GitHub, here 报告为一个问题(link 也包含一个解决方案)
关于 SO 的类似问题:ggplot2 position_dodge affects error bar width and Width of error bars in ggplot2,两者都有一些您可以探索的答案。
因此,您应该将误差条的宽度缩放到每个 DayNum
的行数。因此,在您的 data.frame 中创建一个新列,像这样说 width
:
library(dplyr)
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
然后您必须将此列映射到 ggplot
的 width
美学中,同时从 geom_errorbar
中删除任何 width
属性。
这是一个工作示例,其中包含您的数据,涵盖了所有 3 个问题。
您的数据:
mData <- read.table(
text = "
GroupID DayNum n dMean dMedian dStdDev dStdErr dIQR
Grp1 13 8 207.03 211.45 13.04 4.61 11.73
Grp1 15 8 288.15 274.40 48.98 17.32 33.25
Grp1 18 8 393.50 381.15 63.63 22.50 52.98
Grp1 21 8 507.63 499.80 73.06 25.83 80.88
Grp1 26 8 636.14 614.65 112.53 39.79 206.53
Grp2 13 8 207.05 205.25 41.00 14.50 72.35
Grp2 15 8 142.76 145.60 27.87 9.85 33.70
Grp2 18 8 77.55 82.55 19.44 6.87 22.88
Grp2 21 8 66.38 69.85 20.56 7.27 23.00
Grp2 26 8 67.05 64.20 29.02 10.26 25.48
Grp2 29 8 66.48 63.85 25.95 9.17 19.38
Grp2 33 8 76.96 74.25 25.31 8.95 28.60
Grp3 13 8 207.94 219.65 34.42 12.17 47.18
Grp3 15 8 149.56 155.25 45.74 16.17 70.68
Grp3 18 8 134.83 128.00 59.10 20.90 66.20
Grp3 21 8 164.99 159.40 67.86 23.99 93.63
Grp3 26 8 149.53 160.05 62.48 22.09 100.58
Grp3 29 8 162.21 184.25 61.21 21.64 113.33
Grp3 33 8 177.19 184.00 68.99 24.39 110.35
Grp3 36 8 192.13 160.25 94.93 33.56 122.30",
header = TRUE
)
一个工作示例:
library(dplyr)
library(ggplot2)
# Solves Problem 3 - create helper column width
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
Mypd <- position_dodge(width = 0.2)
ggplot(data = mData,
mapping = aes(x = DayNum,
y = dMean,
color = GroupID,
shape = GroupID,
width = width)) + # map width column to width aesthetic
geom_line(position = Mypd) +
geom_point(position = Mypd,
size = 2) +
# width is inherited from the above mapping, no need to map it again
geom_errorbar(aes(ymin = dMean - mData$dStdDev,
ymax = dMean + mData$dStdDev),
position = Mypd) +
labs(y = bquote(bold("Volume " ~ (mm ^ 3)))) + # Solves Problem 1
# Solves Problem 2:
scale_colour_manual(name = "Group ID", values = c("Grp1" = "blue",
"Grp2" = "red",
"Grp3" = "green")) +
scale_shape_manual(name = "Group ID", values = c("Grp1" = 15,
"Grp2" = 16,
"Grp3" = 17)) +
theme_bw()