我有一个像这样的ggplo条形图
从
创建library(lubridate)
library(ggplot2)
library(grid)
library(scales)
### Set up dummy data.
dayVec <- seq(ymd('2016-01-01'), ymd('2016-01-10'), by = '1 day')
dayCount <- length(dayVec)
dayValVec1 <- c(0,-0.25,0.15,0.3,0.4,0.10,0.17,0.22,0.50,0.89)
dayValVec2 <- c(0.15,0.2,-0.17,0.6,0.16,0.41,0.55,0.80,0.90,1.00)
dayValVec3 <- dayValVec2
dayDF <- data.frame(Date = rep(dayVec, 3),
DataType = factor(c(rep('A', dayCount), rep('B', dayCount), rep('C', dayCount))),
Value = c(dayValVec1, dayValVec2, dayValVec3))
p <- ggplot(dayDF,aes(Date, Value, colour = DataType)) +
theme_bw() +
ggtitle("Chart Title \n") +
scale_color_manual("",values = c("#033563", "#E1E2D2"),labels = c("xxx ", "yyy ")) +
geom_rect(aes(xmin = ymd(min(dayDF$Date)),
xmax = ymd('2016-01-06'),
ymin = -Inf,
ymax = Inf
), fill = "#E1E2D2", alpha = 0.1, colour = "#E1E2D2") +
geom_bar(stat = "identity", fill = "#033563", colour = "#033563") +
geom_hline(yintercept = 0, size = 1) +
scale_x_datetime(expand = c(0,0), labels = date_format('%b-%d'), breaks = date_breaks('1 day')) +
scale_y_continuous(expand = c(0,0), labels = percent, limits = c(min(dayDF$Value)*1.2, max(dayDF$Value)*1.2)) +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(size = 0.5, colour = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
axis.text = element_text(size = 20, colour = "#033563"),
axis.title.x = element_text(hjust = 2),
plot.title = element_text(size = 40, face = "bold", colour = "#033563"),
legend.position = 'bottom',
legend.text = element_text(colour = "#033563", size = 20),
legend.key = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 1.5)
)
p
我现在遇到问题,首先我想让goem_rect从0开始,而不是在第一个栏的中心,这里的问题似乎是日期x轴。其次,我希望有一个像这样的传奇
位于图表的底部。我尝试创建一个虚拟数据系列以获得两个图例条目,但它甚至没有显示图例。如果可以在不弄乱数据的情况下操纵图例,我更愿意。 &#39; yyy&#39;图例条目应指明阴影代表的内容。在此先感谢,我是ggplot2的新手。
答案 0 :(得分:2)
这是一个解决方案:
geom_rect()
的第一个问题。我更喜欢POSIXct
,所以我可以轻松修改x轴:
geom_rect(aes(xmin = as.POSIXct("2015-12-31 12:00:00"),
xmax = as.POSIXct("2016-01-06 12:00:00"),
ymin = -Inf,
ymax = Inf,
fill = "#E1E2D2"),
color = "#E1E2D2", alpha = 0.1)
提示:如果您想完全填写1月06日,请启动"2015-12-31 12:00:00"
展开geom_rect()
并结束"2016-01-06 12:00:00"
。
您应该使用scale_fill_manual()
代替scale_color_manual()
您必须将fill
参数放在aes
geom_rect()
和geom_bar()
内。{/ p>
代码:
library(lubridate)
library(ggplot2)
library(grid)
library(scales)
dayVec <- seq(as.POSIXct('2016-01-01'), as.POSIXct('2016-01-10'), by = '1 day')
dayCount <- length(dayVec)
dayValVec1 <- c(0,-0.25,0.15,0.3,0.4,0.10,0.17,0.22,0.50,0.89)
dayValVec2 <- c(0.15,0.2,-0.17,0.6,0.16,0.41,0.55,0.80,0.90,1.00)
dayValVec3 <- dayValVec2
dayDF <- data.frame(Date = rep(dayVec, 3),
DataType = factor(c(rep('A', dayCount), rep('B', dayCount), rep('C', dayCount))),
Value = c(dayValVec1, dayValVec2, dayValVec3))
dayDF$Date = as.POSIXct(dayDF$Date)
p <- ggplot(dayDF,aes(Date, Value, colour = DataType)) +
theme_bw() +
ggtitle("Chart Title \n") +
geom_rect(aes(xmin = as.POSIXct("2015-12-31 12:00:00"),
xmax = as.POSIXct("2016-01-06 12:00:00"),
ymin = -Inf,
ymax = Inf,
fill = "#E1E2D2"),
color = "#E1E2D2", alpha = 0.1) +
geom_bar(aes(fill = "#033563"), color = "#033563", stat = "identity") +
geom_hline(yintercept = 0, size = 1) +
scale_x_datetime(expand = c(0,0), labels = date_format('%b-%d'), breaks = date_breaks('1 day')) +
scale_y_continuous(expand = c(0,0), labels = percent, limits = c(min(dayDF$Value)*1.2, max(dayDF$Value)*1.2)) +
theme(axis.text.x = element_text(angle = 90),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(size = 0.5, color = "black"),
axis.line = element_line(size = 1),
axis.ticks = element_line(size = 1),
axis.text = element_text(size = 20, color = "#033563"),
axis.title.x = element_text(hjust = 2),
plot.title = element_text(size = 40, face = "bold", colour = "#033563"),
legend.position = 'bottom',
legend.text = element_text(color = "#033563", size = 20),
legend.key = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 1.5)
) +
scale_fill_manual("",values = c("#033563", "#E1E2D2"),labels = c("xxx ", "yyy "))
p
PS:我必须在tz = "Europe/Paris"
中指定scale_x_datetime(label = date_format())
才能拥有正确的日期,具体取决于地点。