基于cut的R二元变量

时间:2017-11-19 11:32:25

标签: r ggplot2

在剪切数据帧之后,如何添加二进制字段(请参阅下面的内容),使得值与剪切交替显示?

 <form action="{{ route('settings.update', 1) }}" method="POST">
                {{ csrf_field() }}
                {{ method_field('PATCH') }}

                <div class="row">
                    <div class="input-field col s4">
                        <input type="text" name="longitude" id="longitude" class="validate" value="{{ old($setting->longitude) ?? $setting->longitude }}">
                        <label class="active" for="longitude">Longitude</label>
                    </div>
                    <div class="input-field col s4">
                        <input type="text"  name="latitude" id="latitude"  class="validate" value="{{ old($setting->latitude) ?? $setting->latitude }}">
                        <label class="active" for="latitude">Latitude</label>
                    </div>
                    <div class="input-field col s4">
                        <select id="zoom" name="zoom">
                            @for($i=1; $i<18; $i++)
                                @if($i == $setting->zoom)
                                    <option value="{{ $i }}" selected>{{ $i }}</option>
                                @else
                                    <option value="{{ $i }}">{{ $i }}</option>
                                @endif
                            @endfor
                        </select>
                        <label class="active" id="zoom">Zoom</label>
                    </div>
                </div>


                <div class="input-field col">
                    <input type="text"  name="address" id="address"  class="validate" value="{{ old($setting->address) ?? $setting->address }}">
                    <label class="active" for="address">Address</label>
                </div>
                <div class="input-field col">
                    <input type="text"  name="email" id="email"  class="validate" value="{{  old($setting->email) ?? $setting->email }}">
                    <label class="active" for="email">Email</label>
                </div>
                <div class="input-field col">
                    <input type="text"  name="tel" id="tel"  class="validate" value="{{ old($setting->tel) ?? $setting->tel }}">
                    <label class="active" for="tel">Telephone</label>
                </div>





                <textarea name="about">{{ old($setting->about) ?? $setting->about }}</textarea>
                <button type="submit" class="btn btn-primary btn-block">Save</button>

            </form>

目标是ggplot一个颜色类似于this的直方图,只有两种颜色: enter image description here

(仅供说明 - 不符合上述值)

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

set.seed(1)
val <- sort(rnorm(100))
df <- data.frame(x = val, cut = cut(val,30))
df$desired <- as.numeric(df$cut) %% 2

head(df, 10)
#            x            cut cut_num desired
# 1  -2.214700  (-2.22,-2.06]       1       1
# 2  -1.989352  (-2.06,-1.91]       2       0
# 3  -1.804959  (-1.91,-1.75]       3       1
# 4  -1.523567   (-1.6,-1.45]       5       1
# 5  -1.470752   (-1.6,-1.45]       5       1
# 6  -1.377060  (-1.45,-1.29]       6       0
# 7  -1.276592  (-1.29,-1.14]       7       1
# 8  -1.253633  (-1.29,-1.14]       7       1
# 9  -1.224613  (-1.29,-1.14]       7       1
# 10 -1.129363 (-1.14,-0.984]       8       0

修改 请注意,剪切的中断可以为空(请参阅示例中的cut_num == 4),在这种情况下,看似连续的中断会获得相同的desired标签。