R 将字符变量(工作日)添加到绘图
我想在这个绘图上添加工作日,作为条形图顶部的文本。 在我找到的ggplot中添加文本的唯一功能是“annotate”,它不能按我想要的方式工作 应该是这样的: geom_text给了我这个 我的代码:R 将字符变量(工作日)添加到绘图,r,text,ggplot2,character,R,Text,Ggplot2,Character,我想在这个绘图上添加工作日,作为条形图顶部的文本。 在我找到的ggplot中添加文本的唯一功能是“annotate”,它不能按我想要的方式工作 应该是这样的: geom_text给了我这个 我的代码: ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)), mapping = aes(as.factor(x = date_days))) + geom_col(m
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)),
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
我的数据头:
distance date_days site T2pn_av T2pn_avambient T2wdir_med weekday Date G2pn_av G2pn_min G2pn_max G2ws_av G2ws_min G2ws_max G2wdir_med
<int> <dttm> <chr> <dbl> <dbl> <dbl> <chr> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -10 2017-07-18 S17N 28814.83 16917.831 110 Di 2017-07-18 13655.29 4621 105100 0.6781284 0 3.6 51.0
2 -10 2017-07-19 S17N 24210.95 15565.951 100 Mi 2017-07-19 10627.73 2908 67250 1.3673618 0 5.5 70.0
3 -10 2017-07-24 S17N 16143.44 7907.442 80 Mo 2017-07-24 11686.54 3582 55080 0.8178753 0 4.8 95.5
4 -10 2017-07-29 S17N 11762.56 5574.563 270 Sa 2017-07-29 12180.73 5413 45490 1.0304985 0 5.7 265.0
5 -10 2017-07-30 S17N 12138.22 6360.225 290 So 2017-07-30 10404.75 6113 23860 1.2385791 0 6.6 274.0
6 -10 2017-07-31 S17N 13815.32 9008.320 270 Mo 2017-07-31 11849.89 4595 46270 0.8554044 0 4.4 230.0
其思想是在每个条的顶部添加一个文本(这就是为什么
vjust=0
,但也可以执行vjust=-.5
以允许更多的空间,或者vjust=1.5
将其放入条中,这也很好)。geom\u文本中的其余部分与geom\u col
中的内容基本相同。但一般来说,您可以将常用美学放在ggplot(aes(…)
中的第一个出现处,就像您对x值所做的那样
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)),
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(width = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
以下内容可以解决标签过多的问题。它采用最高的标签,并将其放置在该x值条的中心。在下面找到添加到数据中的其他行的绘图:
T2G2_dayav <- rbind(T2G2_dayav %>% ungroup(), T2G2_dayav %>% ungroup() %>% mutate(distance = 5)) # add more observations for testing
T2G2_dayav <- T2G2_dayav %>% mutate(T2pn_av = ifelse(distance == 5, T2pn_av/2, T2pn_av)) # label only the highest bar
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)) %>%
group_by(date_days) %>% # group by days
mutate(weekday2 = ifelse(T2pn_av == max(T2pn_av), weekday, NA)), # within each day (group), only label the highest
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday2, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(with = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
这个想法是在每个条的顶部添加一个文本(这就是为什么vjust=0
,但也可以执行vjust=-.5
以允许更多的空间,或者vjust=1.5
将其放入条中,这也很好)。geom\u文本中的其余部分与geom\u col
中的内容基本相同。但一般来说,您可以将常用美学放在ggplot(aes(…)
中的第一个出现处,就像您对x值所做的那样
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)),
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(width = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
以下内容可以解决标签过多的问题。它采用最高的标签,并将其放置在该x值条的中心。在下面找到添加到数据中的其他行的绘图:
T2G2_dayav <- rbind(T2G2_dayav %>% ungroup(), T2G2_dayav %>% ungroup() %>% mutate(distance = 5)) # add more observations for testing
T2G2_dayav <- T2G2_dayav %>% mutate(T2pn_av = ifelse(distance == 5, T2pn_av/2, T2pn_av)) # label only the highest bar
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)) %>%
group_by(date_days) %>% # group by days
mutate(weekday2 = ifelse(T2pn_av == max(T2pn_av), weekday, NA)), # within each day (group), only label the highest
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday2, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(with = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
没问题,请发布您的数据和代码OK我添加了一些详细信息SUSEdput
,以方便我们访问数据以帮助您。在您的情况下,可能是dput(head(T2G2\u dayav))
。可能的重复没有问题,请发布您的数据和代码OK我添加了一些详细信息请使用dput
,以便我们方便地访问数据以帮助您。在您的情况下,可能是dput(head(T2G2\u dayav))
。这可能是非常有用的。但由于每天有9个条,它每天创建9个单独的文本字段。在我的数据提取中,只有距离为-10的观察值。但在整个数据帧中有9种不同的距离。每一段距离都会有一个酒吧。它为每个barI see创建一个文本字段。。。应该仔细看看上面的第一个情节。但是对于将来,您应该确保您提供的示例数据小而简单,并且仍然包含您需要的所有特征。相应地编辑了我的解决方案。这非常有用。但由于每天有9个条,它每天创建9个单独的文本字段。在我的数据提取中,只有距离为-10的观察值。但在整个数据帧中有9种不同的距离。每一段距离都会有一个酒吧。它为每个barI see创建一个文本字段。。。应该仔细看看上面的第一个情节。但是对于将来,您应该确保您提供的示例数据小而简单,并且仍然包含您需要的所有特征。相应地编辑了我的解决方案。