[英]Problems with ggplot - how to set y-axis label to bold, edit legend title and adjust the width of error bars
I created a ggplot function to plot groups of data given below (I am sorry, I do not know how to post the data file) 我创建了一个ggplot函数来绘制下面给出的数据组(很抱歉,我不知道如何发布数据文件) There are three problems with this graph: 该图存在三个问题:
1.The Y-axis title has a superscripted character. 1.Y轴标题带有上标字符。 Because of this, it cannot be bolded (the same formatting for the x-axis title works) 因此,无法将其加粗(x轴标题的格式相同)
2.The Legend title is defined as "Group ID". 2.图例标题定义为“组ID”。 But the code is using the Column Title "GroupID" ignoring the definition. 但是代码使用列标题“ GroupID”忽略了定义。 (The code line "scale_fill_discrete("Group ID")" supposed to set the legend title). (代码行“ scale_fill_discrete(“ Group ID”)“应设置图例标题)。
3.The width of error bars is not constant, it is changing from point to point (easily noticeable for the green ones) 3.误差线的宽度不是恒定的,它随着点的变化而变化(绿色的点很容易注意到)
I appreciate any help to fix these issues. 感谢您为解决这些问题所提供的帮助。
Code: 码:
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
Data: 数据:
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
It is advisable to split your question in several posts when you have several problems. 当您遇到多个问题时,建议将您的问题分为多个帖子。 Meanwhile here is my attempt to help you: 同时,这是我尝试为您提供的帮助:
Replace yLabel = bquote("Volume "~(mm^3))
with yLabel = bquote(bold("Volume " ~ (mm ^ 3)))
(note the bold
). 将yLabel = bquote("Volume "~(mm^3))
替换为yLabel = bquote(bold("Volume " ~ (mm ^ 3)))
(注意, bold
)。 Kind reminder that this problem was addressed in the past here and also here 温馨提示,这个问题在过去的讨论在这里也这里
You do not use any fill aesthetic, so remove scale_fill_discrete("Group ID")
, which has no effect and for each of your aesthetics that you actually use (color and shape), in scale_colour_manual
and scale_shape_manual
add name = "Group ID"
(the desired title of your legend): 您不使用任何填充美学,因此请删除scale_fill_discrete("Group ID")
,该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)
This seems to have been reported as an issue by some on GitHub, here (the link includes a solution as well) 似乎有人在GitHub上将此问题报告为问题,此处 (该链接也包含一个解决方案)
Similar questions on SO: ggplot2 position_dodge affects error bar width and Width of error bars in ggplot2 , both having some answers that you can explore with. 关于SO的类似问题: ggplot2 position_dodge影响误差线的宽度和ggplot2中误差线的宽度 ,它们都有一些可供您探索的答案。
So, you should scale the width of the error bar to the number of row counts for each DayNum
. 因此,您应该将误差线的宽度缩放为每个DayNum
的行数。 So create a new column in your data.frame, say width
like this: 因此,在您的data.frame中创建一个新列,像这样说width
:
library(dplyr)
mData <- mData %>%
group_by(DayNum) %>%
mutate(width = 1.5 * n())
Then you will have to map this column into the width
aesthetic of ggplot
while you delete any width
attribute from geom_errorbar
. 然后,当您从geom_errorbar
删除任何width
属性时, geom_errorbar
将此列映射到ggplot
的width
美感中。
Here is an working example with your data, covering all the 3 problems. 这是一个处理您的数据的示例 ,涵盖了所有3个问题。
Your data: 您的数据:
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
)
An working example: 一个工作示例:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.