动态更改每个条形的高度 & 反转 SVG 元素

这篇博客介绍了如何动态地更改SVG条形图中每个条形的高度,通过将数据点值乘以一个常数进行缩放。还讨论了如何反转SVG元素,使条形图从SVG区域的底部开始向上生长,详细解释了坐标系的原理,并提供了实现这一效果的实操步骤。

动态更改每个条形的高度

介绍

和动态设置 x 值一样,也可以把每个条形的高度设置成数组中数据点的值。

selection.attr("property", (d, i) => {

}) 

d 是数据点值,i 是数组中数据点的索引。

实操

改变 height 属性的回调函数,让它返回数据值乘以 3 的值。

注意: 记住,把所有数据点乘以相同的常数来对数据进行缩放(就像放大), 这有利于看清例子中条形数值之间的差异。

1.第一个 rectheight 应为 36
2.第二个 rectheight 应为 93
3.第三个 rectheight 应为 66
4.第四个 rectheight 应为 51
5.第五个 rectheight 应为 75
6.第六个 rectheight 应为 54
7.第七个 rectheight 应为 87
8.第八个 rectheight 应为 42
9.第九个 rectheight 应为 27

<body><script> const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];const w = 500;const h = 100;const svg = d3.select("body").append("svg").attr("width", w).attr("height", h);svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", (d, i) => i * 30) .attr("y", 0) .attr("width", 25) .attr("height", (d, i) => {
 return d*3 }); </script>
</body> 

反转 SVG 元素

介绍

你可能已经注意到了常见的条形图像是把这个翻转或者颠倒过来。 这是因为 SVG 的 (x, y) 坐标有些特别。

在 SVG 中,坐标轴的原点在左上角。 x 坐标为 0 将图形放在 SVG 区域的左边缘, y 坐标为 0 将图形放在 SVG 区域的上边缘。 x 值增大矩形将向右移动, y 值增大矩形将向下移动。

为了使条形图向上,需要改变 y 坐标计算的方式。 这需要计算条形的高度和 SVG 区域的总高度。

SVG 区域的高度为 100。 如果在集合中一个数据点的值为 0,那么条形将从 SVG 区域的最底端开始(而不是顶端)。 为此,y 坐标的值应为 100。 如果数据点的值为 1,你将从 y 坐标为 100 开始来将这个条形设置在底端, 然后需要考虑该条形的高度为 1,所以最终的 y 坐标将是 99。

y 坐标为 y = heightOfSVG - heightOfBar 会将条形图向上放置。

实操

改变 y 属性的回调函数,让条形图向上放置。 height 的值是 3 倍 d 的值。

注意: 通常,关系是 y = h - m * d,其中 m 是缩放数据点的常数。

1.第一个 recty 值应该为 64
2.第二个 recty 值应该为 7
3.第三个 recty 值应该为 34
4.第四个 recty 值应该为 49
5.第五个 recty 值应该为 25
6.第六个 recty 值应该为 46
7.第七个 recty 值应该为 13
8.第八个 recty 值应该为 58
9.第九个 recty 值应该为 73

<body><script> const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];const w = 500;const h = 100;const svg = d3.select("body").append("svg").attr("width", w).attr("height", h);svg.selectAll("rect") .data(dataset) .enter() .append("rect") .attr("x", (d, i) => i * 30) .attr("y", (d, i) => {return h - (3 * d) }) .attr("width", 25) .attr("height", (d, i) => 3 * d); </script>
</body> 

最后

最近找到一个VUE的文档,它将VUE的各个知识点进行了总结,整理成了《Vue 开发必须知道的36个技巧》。内容比较详实,对各个知识点的讲解也十分到位。



有需要的小伙伴,可以点击下方卡片领取,无偿分享

setwd(&quot;/Volumes/芦苇常用/DPN学习/1.差异分析/第四版差异分析&quot;) A &lt;- KEGG library(ggplot2) library(forcats) library(ggsci) library(dplyr) # 数据处理 A$Description &lt;- as.factor(A$Description) A$Description &lt;- fct_inorder(A$Description) # 按Group分组,在每个分组内按Count降序排列 A_sorted &lt;- A %&gt;% group_by(Group) %&gt;% arrange(desc(Count), .by_group = TRUE) %&gt;% ungroup() %&gt;% mutate(Description = factor(Description, levels = unique(Description))) # 计算标签位置(交换坐标后需调整) nudge_value &lt;- 0.08 * max(A_sorted$Count) # 保持原始计算 # 交换横纵坐标并修改颜色为渐变色 p &lt;- ggplot(A_sorted, aes(x = Count, y = Description)) + # 交换x和y geom_bar( aes(fill = Count), # 改为填充Count值(渐变色基础) stat = &quot;identity&quot;, position = position_dodge(width = 0.75), width = 0.7 ) + # 添加颜色渐变 scale_fill_gradientn( name = &quot;Count&quot;, # 图例名称 colours = c(&quot;#2c7bb6&quot;, &quot;#abd9e9&quot;, &quot;#ffffbf&quot;, &quot;#fdae61&quot;, &quot;#d7191c&quot;), # 蓝-黄-红渐变 values = scales::rescale(c(0, 0.25, 0.5, 0.75, 1)) # 均匀分布锚点 ) + # 文本标签调整(交换坐标后) geom_text( aes(label = Count, group = Group), position = position_dodge(width = 0.75), nudge_x = nudge_value, # 改为水平方向偏移 hjust = 0, # 左对齐 size = 3.5, color = &quot;black&quot;, fontface = &quot;bold&quot; ) + # 坐标轴标签(添加Genetdio) labs( x = &#39;Genetdio&#39;, # 修改x轴标签 y = &#39;&#39; # y轴留空 ) + # 坐标轴扩展 scale_x_continuous(expand = expansion(mult = c(0, 0.15))) + theme_bw() + theme( panel.grid = element_blank(), panel.border = element_blank(), axis.line = element_line(color = &quot;black&quot;), axis.line.x.top = element_blank(), axis.line.y.right = element_blank(), legend.position = &#39;right&#39;, axis.ticks.y = element_blank(), axis.text.y = element_text( size = 12, colour = &quot;black&quot;, margin = margin(r = 15, t = 9, b = 9) # 调整Description文本的边距,增加上下间距 ), axis.text.x = element_text(size = 12), axis.title.x = element_text(size = 15), plot.margin = unit(c(2, 1, 1, 1), &quot;cm&quot;), # 增加边距,尤其是右边距和上边距 aspect.ratio = 1.8 ) # 保存为svg,增加宽度和高度根据Description的数量调整高度,数量多则高度更大) ggsave(&quot;KEGG柱状图.svg&quot;, plot = p, width = 16, height = 17, units = &quot;in&quot;) 修改这个代码,横坐标为Description,纵坐标为Genertio,右边还应有图例是组别,DM VS CN,DPN VS CN,DPN VS DM
10-07
setwd(&quot;/Volumes/芦苇常用/DPN学习/1.差异分析/第四版差异分析&quot;) A &lt;- KEGG library(ggplot2) library(forcats) library(ggsci) library(dplyr) # 数据处理 A$Description &lt;- as.factor(A$Description) A$Description &lt;- fct_inorder(A$Description) # 分组排序 A_sorted &lt;- A %&gt;% group_by(Group) %&gt;% arrange(desc(Count), .by_group = TRUE) %&gt;% ungroup() %&gt;% mutate(Description = factor(Description, levels = unique(Description))) # 创建包含组别和计数的标签列 A_sorted &lt;- A_sorted %&gt;% mutate(Group_Label = paste0(Group, &quot; (&quot;, Count, &quot;)&quot;)) # 计算标签上移距离 nudge_value &lt;- 0.08 * max(A_sorted$Count) # 绘图(修改坐标轴方向) p &lt;- ggplot(A_sorted, aes(x = Description, y = Count)) + # ★修改点1:调整坐标轴方向 geom_bar(aes(fill = Group), stat = &quot;identity&quot;, # ★修改点2:使用Group分组 position = position_dodge(width = 0.75), width = 0.7) + geom_text(aes(label = Count, group = Group), position = position_dodge(width = 0.75), nudge_y = nudge_value, vjust = -0.5, size = 3.5, # ★修改点3:竖直方向偏移 color = &quot;black&quot;, fontface = &quot;bold&quot;) + labs(x = &#39;KEGG Pathway&#39;, y = &#39;Gene Count&#39;) + # ★修改点4:坐标轴标签 scale_fill_npg(name = &quot;Group Comparison&quot;, # ★修改点5:图例标签 labels = c(&quot;DM VS CN&quot;, &quot;DPN VS CN&quot;, &quot;DPN VS DM&quot;)) + # 自定义组别名称 scale_y_continuous(expansion(mult = c(0, 0.15))) + theme_bw() + theme(panel.grid = element_blank(), panel.border = element_blank(), axis.line = element_line(color = &quot;black&quot;), axis.line.x.top = element_blank(), axis.line.y.right = element_blank(), legend.position = &#39;right&#39;, axis.ticks.x = element_blank(), axis.text.x = element_text(size = 12, colour = &quot;black&quot;, angle = 45, hjust = 1, # 旋转文本防止重叠 margin = margin(t = 15, b = 9)), axis.text.y = element_text(size = 12), axis.title = element_text(size = 15), legend.title = element_text(size = 13), legend.text = element_text(size = 12), plot.margin = unit(c(2, 1, 1, 1), &quot;cm&quot;)) + guides(fill = guide_legend(reverse = TRUE)) # ★修改点6:反转图例顺序便于阅读 # 保存 ggsave(&#39;KEGG_plot.svg&#39;, plot = p, width = 16, height = 10, units = &quot;in&quot;) # 调整尺寸比例修改这个代码,图例再加一个Count,三个组三个渐变色,其他的不要动
10-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值