我创建了一个ggplot函数来绘制下面给出的数据组(对不起,我不知道如何发布数据文件) 此图存在三个问题:
1。Y轴标题带有上标字符。因此,无法将其加粗(x轴标题的格式相同)
2。图例标题定义为“组ID”。但是代码使用列标题“ GroupID”忽略了定义。 (代码行“ scale_fill_discrete(“ Group ID”)“应设置图例标题。)
3。误差线的宽度不是恒定的,它随着点的变化而变化(绿色的点很容易看到)
感谢您为解决这些问题所提供的帮助。
代码:
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
答案 0 :(得分:2)
当您遇到多个问题时,建议将您的问题分为几篇。同时,这是我为您提供的尝试:
将yLabel = bquote("Volume "~(mm^3))
替换为yLabel = bquote(bold("Volume " ~ (mm ^ 3)))
(注意bold
)。谨在此提醒您,此问题是在过去的here和here
您不使用任何填充美学,因此请scale_fill_discrete("Group ID")
和{{1}中删除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)
某些on GitHub, here(链接也包含解决方案)似乎已将其报告为问题
关于SO的类似问题:ggplot2 position_dodge affects error bar width和Width of error bars in ggplot2,都有一些可供您探索的答案。
因此,您应该将误差线的宽度缩放为每个DayNum
的行数。因此,在data.frame中创建一个新列,像这样说width
:
library(dplyr)
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
然后,您必须在从width
删除任何ggplot
属性的同时,将此列映射到width
的{{1}}美学中。
这是一个包含您的数据的有效示例,涵盖了所有3个问题。
您的数据:
geom_errorbar
一个有效的例子:
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
)