Skip to content

Commit

Permalink
assign set_color
Browse files Browse the repository at this point in the history
  • Loading branch information
gaospecial committed Dec 26, 2023
1 parent 82ed594 commit b614a79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
27 changes: 14 additions & 13 deletions R/ggVennDiagram.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,18 @@ plot_venn = function(data,
edge_lty = "solid",
edge_size = 1,
...){
p = ggplot(mapping = aes(.data$X, .data$Y))
setedge.params = list(data = get_shape_setedge(data, color = set_color,
linetype = edge_lty,
linewidth = as.numeric(edge_size)),
mapping = aes(color = ifelse(is.character(set_color), I(.data$color), .data$id),
mapping = aes(color = I(.data$color),
group = .data$id,
linetype = I(.data$linetype),
linewidth = I(.data$linewidth)),
show.legend = FALSE)
setlabel.params = list(data = get_shape_setlabel(data, size = as.numeric(set_size), color = set_color),
mapping = aes(label = .data$name,
size = I(.data$size),
color = ifelse(is.character(set_color), I(.data$color), .data$id)),
color = I(.data$color)),
show.legend = FALSE)
region.params = list(data = get_shape_regionedge(data) |> dplyr::left_join(venn_region(data), by = "id"),
mapping = aes(fill = .data$count,
Expand All @@ -141,10 +140,11 @@ plot_venn = function(data,
setlabel.layer = do.call('geom_text', setlabel.params)
region.layer = do.call('geom_polygon', region.params)

p = p + region.layer + setedge.layer + setlabel.layer + theme_void() + coord_equal()
p = ggplot(mapping = aes(.data$X, .data$Y))
p_nonlabel = p + region.layer + setedge.layer + setlabel.layer + theme_void() + coord_equal()

if (label == "none"){
return(p)
return(p_nonlabel)
}

# process data for plotting region labels
Expand All @@ -157,15 +157,16 @@ plot_venn = function(data,
dplyr::rowwise() |>
dplyr::mutate(item = str_wrap(paste0(.data$item, collapse = " "),
width = label_txtWidth))
p = p + geom_text(aes(label = .data$count, text = .data$item),
data = region_label) +
p_plotly = p_nonlabel +
geom_text(aes(label = .data$count, text = .data$item),
data = region_label) +
theme(legend.position = "none")
ax = list(
showline = FALSE
)
p = plotly::ggplotly(p, tooltip = c("text")) |>
p_plotly = plotly::ggplotly(p_plotly, tooltip = c("text")) |>
plotly::layout(xaxis = ax, yaxis = ax)
return(p)
return(p_plotly)
}

# calculate labels, which are 'percent', 'count', or 'both'
Expand All @@ -176,7 +177,7 @@ plot_venn = function(data,

# if label != "none" & show_intersect == FALSE
if (label_geom == "label"){
p = p + geom_label(
p_label = p_nonlabel + geom_label(
aes(label = .data[[label]]),
data = region_label,
alpha = label_alpha,
Expand All @@ -185,19 +186,19 @@ plot_venn = function(data,
lineheight = 0.85,
label.size = NA
)
return(p)
return(p_label)
}

if (label_geom == "text"){
p = p + geom_text(
p_label = p_nonlabel + geom_text(
aes(label = .data[[label]]),
data = region_label,
alpha = label_alpha,
color = label_color,
size = label_size,
lineheight = 0.85
)
return(p)
return(p_label)
}


Expand Down
6 changes: 3 additions & 3 deletions R/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ setMethod("process_data", signature = c(venn = "Venn"),
venn_regionedge = function(obj){
if (!inherits(obj, "VennPlotData")) stop("obj should be a VennPlotData object.")
obj$regionEdge |>
dplyr::left_join(venn_region(obj)) |>
dplyr::left_join(venn_region(obj), by = "id") |>
dplyr::as_tibble()
}

Expand Down Expand Up @@ -145,9 +145,9 @@ process_set_data = function(venn){
if(!inherits(venn, "Venn")) stop("venn is not a S4 class 'Venn' object.")
tibble::tibble(
id = as.character(seq_along(venn@sets)),
name = venn@names,
item = venn@sets,
count = sapply(venn@sets, length),
name = venn@names
count = sapply(venn@sets, length)
)
}

Expand Down
2 changes: 1 addition & 1 deletion R/view_shape.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ VennPlotData = function(x){
print.VennPlotData = function(x, ...){
cat(sprintf("Class VennPlotData - '%s'\n", x$shapeId))
cat(sprintf(" Type: %s; No. sets: %d; No. regions: %d.\n", x$type, x$nsets, length(unique(x$regionEdge$id))))
cat(sprintf(" To view this shape, use `plot_shape_edge()`.\n"))
cat(sprintf(" To view this shape, use `plot_shape_edge(get_shape_by_id('%s'))`.\n", x$shapeId))
cat(sprintf(" To view its components, use `venn_setedge()`, `venn_setlabel()`, etc.\n"))
}

0 comments on commit b614a79

Please sign in to comment.