gpt4 book ai didi

r - geom_polygon 的渐变填充

转载 作者:行者123 更新时间:2023-12-04 10:14:02 25 4
gpt4 key购买 nike

此代码生成一个包含 3 个多边形的图表...

我正在创建一个显示 3 个多边形的图表,如果有更好的方法来绘制多边形(实际上这些多边形代表事件并且这些事件有持续时间),我不是很感兴趣。

首先,我感兴趣的是使用渐变填充每个多边形的可能性。

# library("ggplot2")
# library(data.table)

## some vectors
event.day <- c("A", "A", "B", "B")
event.time <- c(1, 2, 1, 2)
event.duration <- c(1,2,3,1)
sys <- c(100, 50, 50, 100)

## the data data.frame
df.event <- data.frame(event.day, event.time,event.duration,sys)
# ordering the data.frame
df.event <- df.event[with(df.event, order(event.day, event.time)), ]
# sys values of the next event
df.event$sys.end <- c(df.event$sys[-1], NA)
df.event$sys.min <- min(df.event$sys)
df.event$sys.minday <- ave(df.event$sys, list(event.day), FUN=function(x) {min(x)})
df.event$sys.max <- max(df.event$sys)
df.event$sys.maxday <- ave(df.event$sys, list(event.day), FUN=function(x) {max(x)})

# count all events
df.event$cntTotalNoOfEvents <- seq_along(df.event$sys)
# count the events within one day
df.event$cntTotalNoOfEventsByDay <- ave( 1:nrow(df.event), df.event$event.day,FUN=function(x) seq_along(x))
# aggregate the number or events within one day
df.event$TotalNoOfEventsByDay <- do.call(c, lapply(df.event$event.day, function(foo){
sum(df.event$event.day==foo)
}))
# the successor event
df.event$event.successor <- c(df.event$cntTotalNoOfEvents[-1], NA)

df.event$event.day <- factor(df.event$event.day, levels = unique(df.event$event.day))
event.day.level <- levels(df.event$event.day)
df.event$event.day.level.ordinal <- as.numeric(match(df.event$event.day, event.day.level))

## the position data.frame
df.position <- data.frame(event.polygon = rep(c(1:nrow(df.event)), each = 4), polygon.x = 1, polygon.y = 1)
df.position$event.polygon.point <- ave( 1:nrow(df.position), df.position$event.polygon,FUN=function(x) seq_along(x))

## merge of the data and the positition data.frame
dt.polygon <- data.table(merge(df.event, df.position, by.x = "cntTotalNoOfEvents", by.y = "event.polygon"))

## calculating the points of the polygon
dt.polygon[dt.polygon$event.polygon.point == 1, polygon.x := event.day.level.ordinal - .5 * sys / sys.max ]
dt.polygon[dt.polygon$event.polygon.point == 1, polygon.y := cntTotalNoOfEventsByDay]
dt.polygon[dt.polygon$event.polygon.point == 2, polygon.x := event.day.level.ordinal - .5 * sys.end / sys.max]
dt.polygon[dt.polygon$event.polygon.point == 2, polygon.y := cntTotalNoOfEventsByDay + event.duration]
dt.polygon[dt.polygon$event.polygon.point == 3, polygon.x := event.day.level.ordinal + .5 * sys.end / sys.max]
dt.polygon[dt.polygon$event.polygon.point == 3, polygon.y := cntTotalNoOfEventsByDay + event.duration]
dt.polygon[dt.polygon$event.polygon.point == 4, polygon.x := event.day.level.ordinal + .5 * sys / sys.max]
dt.polygon[dt.polygon$event.polygon.point == 4, polygon.y := cntTotalNoOfEventsByDay]

p <- ggplot()

p <- p + geom_polygon(data = dt.polygon
,aes(
x = polygon.x
,y = polygon.y
,fill = sys
,group = cntTotalNoOfEvents
)
)

p <- p + theme(
panel.background = element_rect(fill="white")
)

p <- p + scale_fill_gradient(guide = "colourbar", low = "lightgrey", high = "red")

p <- p + coord_flip()

p

产生这个图表

enter image description here

我想要实现的是这样的

enter image description here

,你有什么想法吗

一如既往,任何提示都值得赞赏

汤姆

最佳答案

嗯,实际上我不确定回答我自己的问题是否有意义......

但是由于我没有收到任何答复,因此我最初的问题可能有点愚蠢。

尽管如此,在最后一天我花了一些时间来解决我的问题。基本上我的解决方案是根据事件的持续时间添加额外的段。我省去了我对持续时间的计算。这是因为我最初的兴趣是如何为多边形提供渐变。

也许你们中的一些人发现我的解决方案很有用

干杯汤姆

library(ggplot2)
library(reshape)
event.day <- c("A", "A", "A", "A", "B", "B")
event <- c(1, 2, 3, 4, 5, 6)
sys <- c(120, 160, 100, 180, 100, 180)
duration <- c(50, 100, 50, 150, 350, 0)
df <- data.frame(event.day, event, sys, duration)
df$end <- c(df$sys[-1], NA)

## replacing na values
df.value.na <- is.na(df$end)
df[df.value.na,]$end <- df[df.value.na,]$sys

## calculating the slope
df$slope <- df$end / df$sys

## creating rows for each event depending on the duration
event.id <- vector()
segment.id <- vector()

for(i in 1:nrow(df)) {
event.id <- c(event.id, rep(df[i,]$event, each = df[i,]$duration))
segment.id <- c(segment.id,c(1:df[i,]$duration))
}

## merging the original dataframe with the additional segments
df.segments <- data.frame(event.id, segment.id)
df <- merge(df, df.segments, by.x = c("event"), by.y = c("event.id"))

## calculate the start and end values for the newly created segements
df$segment.start <- df$sys + (df$segment.id - 1) * (df$end - df$sys) / df$duration
df$segment.end <- df$sys + (df$segment.id) * (df$end - df$sys) / df$duration

## just a simple calculation
value.max <- max(df$sys)

df$high <- 1 + 0.45 * df$segment.end / value.max
df$low <- 1 - 0.45 * df$segment.end / value.max
df$percent <- df$segment.end / value.max
df$id <- seq_along(df$sys)
df$idByDay <- ave( 1:nrow(df), df$event.day,FUN=function(x) seq_along(x))


## how many events in total, necessary
newevents <- nrow(df)

## subsetting the original data.frame
df <- df[,c("event.day", "id", "idByDay", "segment.id", "segment.start", "duration", "segment.end", "high", "low", "percent")]

## melting the data.frame
df.melted <- melt(df, id.vars = c("event.day", "id", "idByDay", "segment.id", "segment.start", "duration", "segment.end","percent"))
df.melted <- df.melted[order(df.melted$id,df.melted$segment.id),]

## this is a tricky one, basically this a self join, of two tables
# every event is available twice, this is due to melt in the previous section
# a dataframe is produced where every event is contained 4 times, except the first and last 2 rows,
# the first row marks the start of the first polygon
# the last row marks the end of the last polygon
df.melted <- rbind(df.melted[1:(nrow(df.melted)-2),],df.melted[3:nrow(df.melted),])
df.melted <- df.melted[order(df.melted$id,df.melted$segment.id),]


## grouping, necessary for drawing the polygons
# the 1st polygon spans from the 1st event, and the first 2 rows from 2nd event
# the 2nd polygon spans from last 2 rows of the 2nd event and the first 2 rows from 3rd event
# ...
# the last polygon spans from the last 2 rows of the next to last event and the 2 rows of the last event
df.melted$grouping <- rep (1:(newevents-1), each=4)
df.melted <- df.melted[order(df.melted$id, df.melted$grouping, df.melted$variable), ]


## adding a 4 point for each group
df.melted$point <- rep(c(1,2,4,3),(newevents-1))
df.melted <- df.melted[order(df.melted$grouping,df.melted$point), ]

## drawing the polygons
p <- ggplot()

p <- p + geom_polygon(data = df.melted
,aes(
x = value
,y =idByDay
,group = grouping
,fill = percent

)
)

p <- p + labs(x = "something", y="something else")

p <- p + theme(
panel.background = element_blank()
#,panel.grid.minor = element_blank()
#axis.title.x=element_blank()
#,axis.text.x=element_text(size=12, face=2, color="darkgrey")
#,axis.title.y=element_blank()
#,axis.ticks.y = element_blank()
#,axis.text.y = element_blank()
)

p <- p + scale_fill_gradient(
low = "lightgrey"
,high = "red"
,guide =
guide_legend(
title = "Sys"
,order = 1
,reverse = FALSE
,ncol = 2
,override.aes = list(alpha = NA)
)
)

p <- p + facet_wrap(~event.day, ncol=2)

p

使用此代码,我能够创建一个如下所示的图表:

enter image description here

关于r - geom_polygon 的渐变填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19987545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com