我想从数据框中绘制100%堆积面积图,其中df的每一行总和为1.示例数据框位于:https://github.com/go-playground/validator/blob/v9/_examples/struct-level/main.go
我最终想要的是这样的: https://pastebin.com/ADMQP6Nx
到目前为止我在SO上找到的大多数解决方案都有不同的data.frame结构,其中分组变量是以行而不是列定义的,例如:
ggplot(data, aes(x=Period, y=Value, fill=Group)) + geom_area()
然后他们就像这样使用ggplot:
var sliderSlide = document.querySelectorAll('.slider__slide');
var nextSlide = document.querySelector('.slider__button--next');
var previousSlide = document.querySelector('.slider__button--previous');
var currentSlide = 0;
var currentSlideImg = 0;
//Reset slides
function resetSlides() {
for (var s = 0; s < sliderSlide.length; s++) {
sliderSlide[s].classList.remove('active');
}
for (var i = 0; i < sliderSlideImg.length; i++) {
sliderSlideImg[i].classList.remove('active');
}
}
//Start slides
function startSlide() {
resetSlides();
sliderSlide[0].classList.add('active');
sliderSlideImg[0].classList.add('active');
}
//Previous slide
function slidePrevious() {
resetSlides();
sliderSlide[currentSlide - 1].classList.add('active');
currentSlide--;
sliderSlideImg[currentSlideImg - 1].classList.add('active');
currentSlideImg--;
}
previousSlide.addEventListener('click', function() {
if (currentSlide === 0 && currentSlideImg === 0) {
currentSlide = sliderSlide.length;
currentSlideImg = sliderSlideImg.length;
}
slidePrevious();
});
//Next slide
function slideNext() {
resetSlides();
sliderSlide[currentSlide + 1].classList.add('active');
currentSlide++;
sliderSlideImg[currentSlideImg + 1].classList.add('active');
currentSlideImg++;
}
nextSlide.addEventListener('click', function() {
if (currentSlide === sliderSlide.length - 1 && currentSlideImg === sliderSlideImg.length - 1) {
currentSlide = -1;
currentSlideImg = -1;
}
slideNext();
});
有没有转换数据框的解决方案?
答案 0 :(得分:4)
使用gather
转换数据:
library(tidyverse)
df <- structure(
list(
Period = 1:4,
t1 = c(0.3, 0.5, 0.1, 0.4),
t2 = c(0.1, 0.1, 0.3,
0.2),
t3 = c(0.4, 0.2, 0.4, 0.3),
t4 = c(0.2, 0.2, 0.2, 0.1)
),
.Names = c("Period", "t1",
"t2", "t3", "t4"),
row.names = c(NA, -4L),
class = "data.frame"
)
df %>%
gather(Group, Value, t1:t4) %>%
ggplot(aes(x=Period, y=Value, fill=Group)) +
geom_area()