在图表区域内的不同的颜色从使用在R的barplot的背景

时间:2016-10-26 13:56:45

标签: r bar-chart

我正在使用R中的“barplot”创建堆积图。我想将图表区域颜色设置为灰色,将背景设置为浅蓝色。我可以将背景颜色设置为浅蓝色但不能控制图表区域内的颜色。有类似的问题要求chartjs但没有barplot.Below是我正在尝试的脚本。有没有办法让我在图表区域内有不同颜色(灰色)的轴和轴外的不同颜色(浅蓝色)?

<?php 
$filter = 'all';
?>
<table>
    <?php 

  // variable declaration
    $total = $project_id = $project_total = 0;

  // fetaching all the tasks
    foreach (get_tasks_list($filter) as $item) {

        // $item['project_id'] --> it has the project_id shows this task belongs to this project
        // this condition checks that current project_id and task's  project id is matched or not
        // at first time $project_id will be zero so this will be true at first time
        // and next time when it will be set to another project id again this will be executed.
        // so in short this condition has 2 purpose
        // 1. show project total
        // 2. show project title
        // it will done once project is changed.                
        if ($project_id != $item['project_id']) {

            // at very first time we will not show total for first project
            // so we are comparing it will 0
            if ($project_id > 0) {

                // print total
                echo '<tr>';
                echo '<th colspan="2">Project Total</th>';
                echo '<th>' . $project_total . '</th>';   
                echo '</tr>';

                // once total is printed we need to reset it for another project
                $project_total = 0;
            }

            // this is needed for printing header only once for each tasks.
            // as if ($project_id != $item['project_id'])
            // this condition only pass when project is changed.
            // so it will print total and then header of project
            // then we will assign $project_id = $item['project_id'] as we need to tell loop
            // current project id is changed or not from previous iteration if its changed then 
            // again print total and header with this condition check if ($project_id != $item['project_id'])
            $project_id = $item['project_id'];
            echo '<thead>';
            echo '<tr>';
            echo '<td>' . $item['project'] . '</td>';
            echo '<td>  Date  </td>';
            echo '<td> Time </td>';
            echo '</tr></thead>';
        }    

        // this will be total for each task and you can see 
        // it is cleared in upper condition when project is changed
        $project_total += $item['time'];

        // this is grand total it will add all the time for all the task and not going to reset
        $total += $item['time'];

        // this will print each task
        echo '<tr>';
        echo '<td>' . $item['title'] .  '</td>';
        echo '<td>' . $item['date'] .  '</td>';
        echo '<td>' . $item['time'] .  '</td>';
        echo '</tr>';
    }
    ?>


    <tr class="totalReportTime">
        <th colspan="2">Total</th>
        <th><?php 
            // at last row we are printing main total
            echo $total; 
        ?></th>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

我能找到的最接近的答案是How to change the background colour of a subplot/inset in R?

但诀窍是绘制图表,主要是为了使轴正确,然后绘制一个矩形,然后在矩形上重绘条形图(仅修改后的代码如下所示):

# Draw barchart to get a plot with correct axes; draw as little as
# possible
barplot(c(foo=1, bar=2.2), names.arg="", col= cols, border = NA, 
  axes = FALSE, ylab = "")

# Draw the background by drawing a rectangle:
rect(par("usr")[1], par("usr")[3], par("usr")[2], 
  par("usr")[4],col = "gray")

# Redraw the barchart with all labels etc. 
barplot(c(foo=1, bar=2.2), main="Figure Title", col= cols, 
  border = TRUE, axes = TRUE, ylab = "YLABEL",
  add=TRUE)