ggplot:使用scale_fill_gradient获取离散图例

时间:2018-06-06 11:54:49

标签: r ggplot2 gradient legend

我试图重新制作一个来自“R Graphics Cookbook”的示例图。第87页。

enter image description here

我尝试使用以下代码重新创建,但我没有得到结果:

// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>

#define SERVER_PORT 8080

void record_video()
{
    /* Record video that has to be transmitted */

}

void handle_request(int sock)
{
    char *hello = "Hello from client";
    char * buffer[1024] = {0};
    int valread;

    printf("Client sock id: %d\n", sock);

    sleep(2);

    /* Connection to the server will be handled here */
    send(sock , hello , strlen(hello) , 0 );
    printf("Hello message sent\n");
    valread = read( sock , buffer, 1024);
    printf("%s\n",buffer );

    close(sock);
}

int main(int argc, char const *argv[])
{
    struct sockaddr_in address;
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char *hello = "Hello from client";
    char buffer[1024] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    memset(&serv_addr, '0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(SERVER_PORT);

    // Convert IPv4 and IPv6 addresses from text to binary form
    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }

    record_video();

    handle_request(sock);
    return 0;
}

给出了

enter image description here

1 个答案:

答案 0 :(得分:2)

问题解决了。事实证明我必须添加指南参数:

sp <- ggplot(diamonds, aes(x=carat, y=price))
    sp + stat_binhex() +
      scale_fill_gradient(low="lightblue", high="red",
                          breaks=c(0, 250, 500, 1000, 2000, 4000, 6000),
                          limits=c(0, 6000), guide = "legend")