diff --git a/R/ggVennDiagram.R b/R/ggVennDiagram.R index 3af5f55..c0fab47 100644 --- a/R/ggVennDiagram.R +++ b/R/ggVennDiagram.R @@ -119,11 +119,10 @@ 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)), @@ -131,7 +130,7 @@ plot_venn = function(data, 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, @@ -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 @@ -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' @@ -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, @@ -185,11 +186,11 @@ 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, @@ -197,7 +198,7 @@ plot_venn = function(data, size = label_size, lineheight = 0.85 ) - return(p) + return(p_label) } diff --git a/R/process_data.R b/R/process_data.R index eda5a51..b792e2d 100644 --- a/R/process_data.R +++ b/R/process_data.R @@ -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() } @@ -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) ) } diff --git a/R/view_shape.R b/R/view_shape.R index bed9fe2..4b63957 100644 --- a/R/view_shape.R +++ b/R/view_shape.R @@ -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")) }