r中的somatoChart(plot或ggplot)

时间:2018-04-07 05:33:25

标签: r ggplot2 plotly

我正在尝试制作一个somatoChart的复制品,以表征一些运动员的体型,我需要一些调整才能得到我期望的结果,想出我的代码,想出我希望的结果。

enter image description here

x <- c(0,-6,6,0,-4.5,4.5,0)
y <- c(12,-6,-6,0,4.5,4.5,-7.5)
par(mar = c(2,0,0,2), mgp = c(2,1,0))
plot(x,y,pch = 20,xlab = " ",ylab = " ",xlim = c(-8,8), ylim = c(-10,16),las = 1, col = "white",axes = F)
axis(4, las = 1, yaxp = c(-10,16,13),cex.axis=0.8)
axis(1, xaxp = c(-8, 8, 16),cex.axis=0.8)
# Segmentes
segments(x0 = 0, y0=-7.5, x1 = 0, y1 = 12, lty = 2)
segments(x0 = -6, y0=-6, x1 = 4.5, y1 = 4.5, lty = 2)
segments(x0 = 6, y0=-6, x1 = -4.5, y1 = 4.5, lty = 2)
# text
windowsFonts(B=windowsFont("Bookman Old Style"))
text(0,13,"MESOMORPH", cex = 0.6,family="B", font = 2)
text(-6,-8,"ENDOMORPH",cex = 0.6,family="B", font = 2)
text(6,-8,"ECTOMORPH", cex = 0.6,family="B", font = 2)
# curves
segments(x0 = -4.5, y0=4.5, x1 = 0, y1 = 12)
segments(x0 = -4.5, y0=4.5, x1 = -6, y1 = -6)
segments(x0 = 0, y0=-7.5, x1 = -6, y1 = -6)
segments(x0 = 0, y0=-7.5, x1 = 6, y1 = -6)
segments(x0 = 4.5, y0=4.5, x1 = 6, y1 = -6)
segments(x0 = 4.5, y0=4.5, x1 = 0, y1 = 12)

请读者提出建议吗?

2 个答案:

答案 0 :(得分:3)

也许使用xspline可以解决您的问题:

x <- c(0,-6,6,0,-4.5,4.5,0)
y <- c(12,-6,-6,0,4.5,4.5,-7.5)
par(mar = c(2,0,0,2), mgp = c(2,1,0))
plot(x,y,pch = 20,xlab = " ",ylab = " ",xlim = c(-8,8), ylim = c(-10,16),las = 1, col = "white",axes = F)
axis(4, las = 1, yaxp = c(-10,16,13),cex.axis=0.8)
axis(1, xaxp = c(-8, 8, 16),cex.axis=0.8)
# Segmentes
segments(x0 = 0, y0=-7.5, x1 = 0, y1 = 12, lty = 2)
segments(x0 = -6, y0=-6, x1 = 4.5, y1 = 4.5, lty = 2)
segments(x0 = 6, y0=-6, x1 = -4.5, y1 = 4.5, lty = 2)
# text
windowsFonts(B=windowsFont("Bookman Old Style"))
text(0,13,"MESOMORPH", cex = 0.6,family="B", font = 2)
text(-6,-8,"ENDOMORPH",cex = 0.6,family="B", font = 2)
text(6,-8,"ECTOMORPH", cex = 0.6,family="B", font = 2)

xspline(y = c(-6, 4.5, 12), x =  c(-6, -4.5, 0), shape = -1, lty = 2)
xspline(y = c(-6, -7.5,-6), x =  c(-6, 0, 6), shape = -1, lty = 2)
xspline(y = c(-6, 4.5, 12), x = c(6, 4.5, 0),  shape = -1, lty = 2)

enter image description here

答案 1 :(得分:0)

以防万一有人来这里寻找如何制作 somatochart 图表,我在这里分享我用 Highcharts 做的一个

https://jsfiddle.net/aledc/1m89wju0/2/

和代码:

HTML:

<script src="https://code.highcharts.com/highcharts.js"></script>

<div id="somatocarta" style="height:450px;width:500px;"></div>

JAVASCRIPT:

var data = [
         {"x":0,"y":0},      // en el Centro
         {"x":-10,"y":30},   // cuadrante Superioro Alto Izquierdo
         {"x":15,"y":35},    // cuadrante Superior Alto Derecho
         {"x":45,"y":13},    // cuadrante Superior Medio Derecho
         {"x":52,"y":-13},   // cuadrante Inferior Medio Derecho
         {"x":25,"y":-33},   // cuadrante Inferior Bajo Derecho
         {"x":-25,"y":-33},  // cuadrante Inferior Bajo Izquierdo
         {"x":-42,"y":-14},  // cuadrante Inferior Medio Izquierdo   
         {"x":-42,"y":14},   // cuadrante Superior Medio Izquierdo

    ]

    //alert(JSON.stringify(data));


    Highcharts.chart('somatocarta', {
    chart: {
        plotBackgroundImage: 'https://raw.githubusercontent.com/aledc7/Laravel/master/resources/somatocarta.png',
        renderTo: 'somatocarta',
            defaultSeriesType:'scatter',
            borderWidth:1,
            borderColor:'#ccc',
            marginLeft:90,
            marginRight:50,
    },

    title:{
            text:'Somatocarta'
        },
        legend:{
            enabled:false                                
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                    this.x +': '+ this.y;
            }
        },
        plotOptions: {
            series: {
                shadow:false,
            }
        },
        xAxis:{
            title:{
                text:'X Axis Title'
            },
            min:-100,
            max:100,
            tickInterval:100,
            tickLength:0,
            minorTickLength:0,
            gridLineWidth:1,
            showLastLabel:true,
            showFirstLabel:false,
            lineColor:'#ccc',
            lineWidth:1                
        },
        yAxis:{
            title:{
                text:'Y Axis<br/>Title',
                rotation:0,
                margin:25,
            },
            min:-100,
            max:100,
            tickInterval:100,
            tickLength:3,
            minorTickLength:0,
            lineColor:'#ccc',
            lineWidth:1        
        },
        series: [{
            color:'#185aa9',
            data: data
        }]
});